@JsonIdentityInfo导致不希望的结果

时间:2017-02-17 08:13:59

标签: spring-mvc spring-boot spring-security jackson relationship

我使用hibernate orm来映射RolePrivilege之间的多对多关系,如下所示:

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<Privilege> privilegeCollection;

    @ManyToMany(mappedBy = "roleCollection")
    @WhereJoinTable(clause = "privilege_privilege_id IN (SELECT ph.parent_privilege FROM privilege_hierachy ph)")
    private Collection<Privilege> parentPrivilegeCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "roleId")
    @JsonIgnore
    private Collection<User> 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<Role> roleCollection;

    @JoinTable(name = "privilege_hierachy", joinColumns = {
            @JoinColumn(name = "parent_privilege", referencedColumnName = "privilege_id")}, inverseJoinColumns = {
            @JoinColumn(name = "child_privilege", referencedColumnName = "privilege_id")})
    @ManyToMany
    private Collection<Privilege> privilegeCollection;

    public Privilege() {
    }
    // Getter and Setters removed for brevity
}

我想得到的是{J}中各自Role的所有Privilege。显然会带回StackOverflowError,因为你可以看到Role  转到Privilege,然后返回Role,依此类推。

因为是多对多双向的,我用@JsonIdentityInfo关系的两边注释它。

期望的结果是获得没有循环stackoverflower的特权的角色,但结果是Json如下。我不明白的是privilegeCollection中的物品有些物品很好,但为什么数字(57,97,165,161,124)......那些来自......为什么不是物品

enter image description here

0 个答案:

没有答案