Petr Mensik
Petr Mensik

Reputation: 27496

ManyToMany mapping in JPA

I have a M:N relationship between table Users and Groups. Now I am trying to join those two tables using JPA and I always get this exception:

Multiple writable mappings exist for the field [GROUPS.name]. Only one may be defined as writable, all others must be specified read-only.

Here is my Users class (I didn't enclose getters and setters for brevity), it's implemented by Admin class and SignedUser class with some additional properties.

@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;

and this is the Group class:

@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;

I've also tried to put JoinTable annotation into Users class but it ended with the same result. Thanks in advance for any advices.

Upvotes: 1

Views: 495

Answers (1)

madth3
madth3

Reputation: 7344

In the class Group you have two mappings for column name as properties groupName and descr.

The error is not related to the ManyToMany.

Upvotes: 3

Related Questions