Next task was to get it work like this (I want to be able to select different roles for one user):
itemLabel="name" itemValue="id" id="select_role" data-placeholder="Choose a Role..." class="form-control chosen-select" multiple="true" tabindex="4" >
And yes.... here is my entity definition:
@Entity
@Table(name="member", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Member implements Serializable {
/** Default value included to remove warning. Remove or modify at will. **/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(name = "company_id")
private Company company;
@NotNull
@Size(min = 1, max = 25)
@Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces")
private String name;
@JsonIgnore
private String password;
@NotNull
@NotEmpty
private String email;
@NotNull
@Size(min = 10, max = 12)
@Digits(fraction = 0, integer = 12)
@Column(name = "phone_number")
private String phoneNumber;
@JsonIgnore
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "member_roles", joinColumns = {
@JoinColumn(name = "MEMBER_ID", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "ROLE_ID",
nullable = false, updatable = false) })
private Set
public Set
return roles;
}
public void setRoles(Set
this.roles = roles;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
And role:
@Entity
@Table(name = "role")
public class Role implements Serializable {
/** Default value included to remove warning. Remove or modify at will. **/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@NotNull
@Size(min = 1, max = 25)
@Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonIgnore
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "roles")
private Set members = new HashSet(0);
public Set getMembers() {
return members;
}
public void setMembers(Set members) {
this.members = members;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
But when I try to put this at form I got conversation error because spring try to convert representation of role at string to role object itself. I had to teach Spring how to do that conversation:
public class StringToRole implements Converter {
@Autowired
private RoleDao roleDao;
@Override
public Role convert(String roleid) {
Role role = roleDao.findById(Long.valueOf(roleid));
return role;
}
}
And register converter in spring context:
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">