我想使用 JPA 2.0 实现以下关系。实体图
简而言之:一个用户在一个组中可以有多个角色。
有没有方便的方法来实现这一点?一个代码示例会很好。
提前致谢!
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "id_usuario")
private Integer idUsuario;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 80)
@Column(name = "nombre")
private String nombre;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "Users")
private Collection<NzSistemaControl> nzSistemaControlCollection;
角色
public class Roles implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id_rol")
private Integer idRol;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "nombre")
private String nombre;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "Roles")
private Collection<NzSistemaControl> nzSistemaControlCollection;
关系表
public class SistemaControl implements Serializable {
@EmbeddedId
protected NzSistemaControlPK nzSistemaControlPK;
@JoinColumn(name = "id_usuario", referencedColumnName = "id_usuario_sistema", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Users user;
@JoinColumn(name = "id_rol", referencedColumnName = "id_rol", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Roles roles;
关系PK
public class NzSistemaControlPK implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "id_usuario")
private int idUsuario;
@Basic(optional = false)
@NotNull
@Column(name = "id_rol")
private int idRol;