For example User-Role-Privilege entities:
@Entity
public class CmsAccount {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false, unique = true)
private String userId;
@Column(nullable = false)
private String password;
private String fullName;
private String countryCode;
private String siteCode;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "cmsuser_roles",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
private Collection<Role> roles = new ArrayList<>();
@NotNull
private Boolean suspended = false;
}
@Entity
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true)
private String name;
@ManyToMany(mappedBy = "roles")
private Collection<CustomerAccount> customerAccounts;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "roles_privileges",
joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id"))
private Collection<Privilege> privileges = new ArrayList<>();
public void addCustomer(CustomerAccount customer) {
customerAccounts.add(customer);
}
public void addPrivilege(Privilege priv) {
privileges.add(priv);
}
}
@Entity
public class Privilege {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true)
private String name;
public Privilege(String name) {
this.name = name;
}
}
Hope, it will help you
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…