【问题标题】:Define custom fields for some users为某些用户定义自定义字段
【发布时间】:2013-11-15 14:36:30
【问题描述】:

对于我的应用程序,我需要定义这些类型的配置文件:

  • 推销员
  • 客户
  • 管理员

用户可以拥有一个或所有这些配置文件。这就是我没有选择继承设计的原因,我更喜欢选择角色策略,这样一个用户可以拥有多个角色。例如,用户可以具有推销员和客户角色。问题是每个角色都有一些具体信息,例如销售员必须指定他的送货地址......

这是我使用 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 是通用的,我不想在一个角色中混合所有角色的字段,我更喜欢将每个特定字段封装在每个角色中。我不想将所有角色的所有字段混合在数据库的一个表中,因为每个角色都有他的具体约束。 我该怎么办?

【问题讨论】:

    标签: jpa user-roles


    【解决方案1】:

    我找到了一个平均解决方案,我可以为每种配置文件使用组合。

        @Entity
        @Table(name = "USER")
        public class User implements UserDetails{
    
        @Id 
        @Column(name = "USER_ID")
        private Long id;
    
        @OneToMany
        private SalesmanProfile salesmanProfile;
    
        @OneToMany
        private CustomerProfile customerProfile;
    
        public Long getId() {
            return id;
        } 
        public void setId(Long id) {
            this.id = id;
        } 
    }
    
    
    @Entity
    @Table(name = "SALESMAN_PROFILE")
    public class SalesmanProfile{
    
        @Id
        @Column(name = "SALESMAN_PROFILE_ID")
        private Long id;
    
        @ManyToOne
        @JoinColumn(name = "USER_ID", nullable = false)
        private User user;
    
        private SpecificsFields.....
    
    }
    

    CustomerProfile 也一样...

    如果我想知道用户是否是推销员,我可以检查用户的推销员资料是否不为空... 你怎么看?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      相关资源
      最近更新 更多