I have the following tables Customers, Roles, CustomerRoles. Customers and Roles have a Long PK and CustomerRoles obviously does the many to many mappings. Consider the Roles table to be a fixed table (but not hard coded) defined by the system. I.e. a table driven "enum". I.e. "Admin", "User", etc.
On the Java side, I have a Customer entity and a RoleEntity and I'm using Hibernate / JPA to map.
It's all working how it is now, but I wind up with Json that looks like this:
{
"customerId": 100000,
"firstName": "Bob",
"lastName": "Jenkins",
"roles": [
{
"name": "Admin"
},
{
"name": "Super User"
}
]
},
What I really want is for it to look like:
"roles": [ "Admin", "Super User" ]
and internally have it FK'ed by the M2M table using the ids. Note the roleid field is set to json ignore, but it still leaves it as a object array rather then a string array.
Obviously, the strings need to be enforced against whats in the Roles table.
Any suggestions?
Customer entity:
@ApiModelProperty(notes="Id of the customer.", required=true, value="100000")
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonProperty(access=Access.READ_ONLY)
private Long customerId = 0L;
@NotNull
@Size(min=2, max=64)
@ApiModelProperty(notes="First name of the customer.", required=true, value="John")
private String firstName;
@NotNull
@Size(min=2, max=64)
@ApiModelProperty(notes="Last name of the customer.", required=true, value="Smith")
private String lastName;
@ManyToMany(cascade={ CascadeType.PERSIST })
@JoinTable(name="CustomerRoles",
joinColumns={ @JoinColumn(name="CustomerId") },
inverseJoinColumns={ @JoinColumn(name="RoleId") }
)
private List<Role> roles = new ArrayList<>();
public Long getCustomerId() {
return this.customerId;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<Role> getRoles() {
//return new ArrayList<Role>(this.roles);
return this.roles;
}
Role entity:
@ApiModelProperty(notes="Id of the role.", required=true, value="1")
@Id
//@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long roleId;
@NotNull
@Size(min=2, max=64)
@ApiModelProperty(notes="Name of the role.", required=true, value="Admin")
private String name;
@JsonIgnore
public Long getRoleId() {
return this.roleId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}