简体   繁体   中英

How to use JPA to generate jointable where an entity joins with a join table?

I'm trying to implement the JPA Entity POJOs for a User-Role-Team-Group model from this SO question

Here is an ERD for the schema:

ER Diagram

I am not able to figure out the entity model in JPA.

Here is some code that I am working on

Account Entity

@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;

Role Entity

@Entity
public class Role extends BaseEntity {

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

The BaseEntity is just common fields with a @PrePersist and @PreUpdate

How do I add Teams to the mix here using the jointable between Account and Roles for instance?

Update :

I was able to get the desired table structure using the below code:

Accounts:

@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<>();

Groups:

@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;

Roles:

@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<>();

Teams:

@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;

But, I'm not sure how do I navigate the chain.

For instance how do I get Account -> Role -> Team -> Group

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM