我想使用JPA 2.0实现以下关系。 Entity Diagram
简而言之:用户可以在一个组中拥有多个角色
有没有方便的方法来实现这一目标?一个代码示例会很好。
提前致谢!
答案 0 :(得分: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;
RelationShipPK
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;