如何使用JPA生成实体与连接表连接的连接表?

时间:2017-04-17 21:19:37

标签: java hibernate jpa

我试图通过此SO question

为用户角色 - 团队 - 组模型实施JPA实体POJO

以下是架构的ERD:

ER Diagram

我无法弄清楚JPA中的实体模型。

以下是我正在处理的一些代码

帐户实体

@Entity
public class Account extends BaseEntity {

@Id
@GeneratedValue
private Long accountId;
private String username;
private String password;
private boolean accountNonExpired;
private boolean accountNonLocked;
private boolean credentialsNonExpired;
private boolean enabled;

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "ACCOUNT_ROLE", joinColumns = @JoinColumn(name = "ACCOUNT_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
private Set<Role> userRoles;

角色实体

@Entity
public class Role extends BaseEntity {

@Id
@GeneratedValue
private Long roleId;
private String roleName;

BaseEntity只是@PrePersist和@PreUpdate的常见字段

如何使用帐户和角色之间的连接表在此处添加团队?

更新:

我能够使用以下代码获得所需的表结构:

账户:

@Entity
public class Accounts extends BaseEntity {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;
private String username;
private String password;
private boolean accountNonExpired;
private boolean accountNonLocked;
private boolean credentialsNonExpired;
private boolean enabled;
private boolean ldapManaged;


@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
private List<AccountsRoles> roles = new ArrayList<>();

组:

@Entity
public class Groups extends BaseEntity {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;
private String name;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "ACCOUNTS_ROLES_GROUPS")
private List<AccountsGroups> accounts;

角色:

@Entity
public class Roles extends BaseEntity {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;
private String roleName;

@OneToMany(mappedBy = "role", cascade = CascadeType.ALL, orphanRemoval = true)
private List<AccountsRoles> accounts = new ArrayList<>();

小组:

@Entity
public class Teams extends BaseEntity{

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;
private String name;
private String managerEmail;
private String teamLeadEmail;
private String distributionListEmail;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "ACCOUNTS_ROLES_TEAMS")
List<AccountsRoles> accounts;

AccountsGroups:

@Entity
public class AccountsGroups implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;


@Id
@ManyToOne
private Accounts account;

@Id
@ManyToOne
private Groups group;

}

AccountsRoles:

@Entity
public class AccountsRoles implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@ManyToOne
private Accounts account;

@Id
@ManyToOne
private Roles role;

但是,我不知道如何导航链。

例如我如何获得帐户 - &gt;角色 - &gt;团队 - &gt;小组

1 个答案:

答案 0 :(得分:0)

@Entity
public class Account extends BaseEntity {
...
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<AccountRole> userRoles;
}

@Entity
public class AccountRole {
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER) //don't delete with cascade
@JoinColumn(name = "account_id", nullable = false)
private Account account;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER) //don't delete with cascade
@JoinColumn(name = "role_id", nullable = false)
private Role role;
}