【问题标题】:JPA 2.0: Mapping between three entitiesJPA 2.0:三个实体之间的映射
【发布时间】:2013-04-22 14:48:59
【问题描述】:

我想使用 JPA 2.0 实现以下关系。 Entity Diagram
简而言之:一个用户可以在一个组中拥有多个角色。
有没有方便的方法来实现这一点?一个代码示例会很好。

提前致谢!

【问题讨论】:

  • 实体图打不开!

标签: jpa-2.0


【解决方案1】:
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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多