JPA中的ManyToMany映射

时间:2011-12-07 00:58:25

标签: java database jpa-2.0

我在表格UsersGroups之间有M:N关系。现在我尝试使用JPA加入这两个表,我总是得到这个例外:

  

字段[GROUPS.name]存在多个可写映射。只有一个可以被定义为可写,所有其他的必须被指定为只读。

这是我的Users课程(为简洁起见,我没有附上getter和setter),它由Admin类和SignedUser类实现,并带有一些其他属性。

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Users implements Serializable {

    @Id
    @Column(name = "login", nullable = false, length = 10)
    private String login;

    @Column(name = "name", nullable = false, length = 30)
    private String name;

    @Column(name = "surname", nullable = false, length = 50)
    private String surname;

    @Column(name = "email", nullable = false, length = 100)
    private String email;

    @Column(name = "password", nullable = false, length = 20)
    private String password;

    @ManyToMany(mappedBy="users")
    private List<Groups> groups;

这是Group类:

@Entity
public class Groups implements Serializable {

    @Id
    @Column(name = "name", nullable = false, length = 20)
    private String groupName;

    @Column(name = "name", nullable = false, length = 50)
    private String descr;

    @ManyToMany
    @JoinTable(name = "user_group",
               joinColumns = {@JoinColumn(name = "groupName")},
               inverseJoinColumns = {@JoinColumn(name = "login")})
    private List<Users> users;

我还尝试将JoinTable注释放入Users类,但结果却相同。提前感谢任何建议。

1 个答案:

答案 0 :(得分:3)

在班级Group中,列name有两个映射作为属性 groupName descr

该错误与ManyToMany无关。