I’m having problems making @Column(name=”example”) working.
I’ve got a User class:
@Entity
public class User {
@Id
private String username;
....
}
A Role one:
@Entity
public class Role {
@Id
private String name;
....
}
That are in a ManyToMany relationship. So I created a RoleMembership:
@Entity
@IdClass(UserRolePK.class)
public class RoleMembership {
@Id
@ManyToOne
@PrimaryKeyJoinColumn(name="USERNAME")
private User user;
@Id
@ManyToOne
@PrimaryKeyJoinColumn(name="ROLE")
private Role role;
....
}
As you can see, the primary key is defined in UserRolePK:
public class UserRolePK{
@Id
@Column(name="ROLE")
private String role;
@Id
@Column(name="USERNAME")
private String user;
...
}
In this class I use @Column(name=”USERNAME”) and @Column(name=”ROLE”) to force its name to that string, but it’s not working: JPA gives it the default names USER_USERNAME and ROLE_NAME (that are in TABLE_ID format).
Can anyone help me finding the mistake?
Thanks,
Andrea
EDIT:
I need to have three tables:
- user (username (pk), password ….)
- user_role (username, role_name)
- role (name (pk), description)
I cannot change User definition in my model.
Remove all the annotations in
UserRolePKand change your@PrimaryKeyJoinColumnannotations to@JoinColumnannotations.