对于我的应用程序,我需要定义这些类型的配置文件:
- 推销员
 - 顾客
 - 行政人员
 
用户可以拥有一个或所有这些配置文件。这就是为什么我没有选择继承设计,我更喜欢选择角色策略,所以一个用户可以有很多角色。例如,用户可以具有推销员和客户角色。问题是每个角色都有一些具体信息,例如销售员必须指定他的送货地址......
这是我使用 spring security/JPA 的实现:
用户模型
@Entity
@Table(name = "USER")
public class User implements UserDetails{
    @Id 
    @Column(name = "USER_ID")
    private Long id;
     @ManyToMany(fetch = FetchType.EAGER)
     @JoinTable(
          name="USER_ROLE",
          joinColumns={@JoinColumn(name="USER_ID", referencedColumnName="USER_ID")},
          inverseJoinColumns={@JoinColumn(name="USER_ROLE_ID", referencedColumnName="ROLE_ID")})
    private Set<Role> roles = new HashSet<Role>();
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Set<Role> getRoles() {
        return roles;
    }
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.addAll(roles);
        return authorities;
    } 
}
好榜样
@Entity
@Table(name = "ROLE")
public class Role implements GrantedAuthority {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ROLE_ID")
    private Long idRole;
    @Column(name = "TYPE_ROLE")
    private String typeRole;
    public Long getIdRole() {
        return this.idRole;
    }
    public void setIdRole(Long idRole) {
        this.idRole = idRole;
    }
    public String getTypeRole() {
        return this.typeRole;
    }
    public void setTypeRole(String typeRole) {
        this.typeRole = typeRole;
    }
    @Override
    public String getAuthority() {
        return typeRole;
    } 
}
我不能为每个角色定义特定字段,因为类 Role 是通用的,我不想在一个角色中混合所有角色的字段,我更喜欢将每个特定字段封装在每个角色中。我不想将所有角色的所有字段混合在数据库的一个表中,因为每个角色都有他的具体限制。我应该怎么做 ?