For my application, i need to define these kind of profiles :
- Salesman
- Customer
- Administrator
A user can have one or all these profiles. It's why i didn't choose the inheritance design, I prefered to choose a role strategy, so a user can have many roles. For example, a user can have salesman and customer role. The problem is that there are some specifics informations for each role, for example a salesman must specify his delivery adresses...
Here is a my implementation with spring security/JPA :
User model
@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;
}
}
role model
@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;
}
}
I cannot define specific fields for each role because the class Role is generic, i don't want to mix fields of all role in one role, i prefer to encapsulate each specific fields in each role. I don't want too store all fields of all role mixed in one table in database because each role have his specifics constraints. How should i do ?