I use hibernate orm to map have a many-to-many relationship between Role and Privilege like so:
Role
@Entity
@Audited
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "roleId")
public class Role extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "role_id")
private Integer roleId;
@Size(max = 45)
@Column(name = "role")
private String role;
@ManyToMany(mappedBy = "roleCollection")
private Collection privilegeCollection;
@ManyToMany(mappedBy = "roleCollection")
@WhereJoinTable(clause = "privilege_privilege_id IN (SELECT ph.parent_privilege FROM privilege_hierachy ph)")
private Collection parentPrivilegeCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "roleId")
@JsonIgnore
private Collection userCollection;
public Role() {
}
//Getters and Setters removed for brevity
}
Privilege
@Entity
@Audited
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "privilegeId")
public class Privilege extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "privilege_id")
private Integer privilegeId;
@Size(max = 45)
@Column(name = "name")
private String name;
@Size(max = 150)
@Column(name = "description")
private String description;
@Size(max = 45)
@Column(name = "friendly_name")
private String friendlyName;
@JoinTable(name = "role_privilege", joinColumns = {
@JoinColumn(name = "privilege_privilege_id", referencedColumnName = "privilege_id")}, inverseJoinColumns = {
@JoinColumn(name = "role_role_id", referencedColumnName = "role_id")})
@ManyToMany
private Collection roleCollection;
@JoinTable(name = "privilege_hierachy", joinColumns = {
@JoinColumn(name = "parent_privilege", referencedColumnName = "privilege_id")}, inverseJoinColumns = {
@JoinColumn(name = "child_privilege", referencedColumnName = "privilege_id")})
@ManyToMany
private Collection privilegeCollection;
public Privilege() {
}
// Getter and Setters removed for brevity
}
What i want to get is all Roles with their respective Privileges in JSON. Obviously that brings back StackOverflowError as you can see Role
goes to Privilege which goes back to Role and so on.
Because is many-to-many bidirectional i annotated it with @JsonIdentityInfo both sides of the relation.
The desired result is to get roles with privileges without cyclic stackoverflowerror, but what is get as result is Json below. What i dont understand is items in privilegeCollection some are objects which is fine but why numbers (57,97,165,161,124)... where are those comming from...why not objects

