Для одного ко многим, вы можете иметь как ниже в вашем классе сущности:
@OneToMany
в сущности пользователя
@OneToMany(mappedBy = "user")
private List<Role> roles;
@ManyToOne
на ролевой стороне
@ManyToOne
@JoinColumn(name = "user")
private User user;
@ ManyToMany
@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(name = "user_role",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private List<Role> roles= new ArrayList<>();
и на ролевой стороне:
@ManyToMany(mappedBy = "role")
private List<User> users = new ArrayList<>();
вот пример, когда многие со многими с emdeded:
@Entity
public class UserRoleMaster implements Serializable {
@EmbeddedId
private UserRoleMasterId id;
@ManyToOne
@JoinColumn(name = "user_master_id", referencedColumnName = "user_master_id", insertable = false, updatable = false)
private UserMaster userMaster;
@ManyToOne
@JoinColumn(name = "user_roles_id", referencedColumnName = "user_roles_id", insertable = false, updatable = false)
private UserRoles userRoles;
//getter //setter //constructures
@Embeddable
public static class UserRoleMasterId implements Serializable {
@Column(name = "user_master_id")
protected Long userMasterId;
@Column(name = "user_roles_id")
protected Long userRolesId;
//constrcture //getter //setters
}
}