在定义具有不同角色/权限的用户(例如具有受限访问权限的普通用户和具有完全访问权限的管理员)时,有哪些最佳实践?
我的用户类看起来像这样:
@Entity
@Table(name = "users")
public class User {
@Id
@Column(name = "userID")
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long userID;
public Long getUserID() {
return userID;
}
@ManyToOne(fetch = FetchType.LAZY)
@Column(nullable = false)
private Role role;
@Column(nullable = false)
private boolean isActive;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String password;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Calendar lastLoggedIn;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Calendar createdDate;
@Version
private Integer version;
}
我的角色类是:
@Entity
@Table(name = "roles")
public class Role {
@Id
@Column(name = "roleID")
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long roleID;
@Column(nullable = false)
private String roleName;
@Column(nullable = false)
private String rolePermissions;
}
假设我的应用程序需要检索用户及其角色的列表,用户实例是否应该持有对角色实例(或代理)的引用?如果我的应用程序必须查找特定角色的所有用户怎么办,角色是否必须具有用户列表?设计和实施这种关系的一些尝试和真实的方法是什么?
另外,我是否使用 JPA 正确进行映射?