REST中的太多深度导致spring-data-neo4j的自我关系

时间:2016-07-08 17:10:04

标签: rest neo4j spring-data-neo4j neo4j-ogm

我有自我关系(工作人员可以向经理报告,经理可能有很多员工)如下:

@NodeEntity
public class Employee {

    @GraphId
    private Long id;

    private String name;
    private String sn;
    private String mail;

    public Employee() {
        super();
    }

    public Employee(String name, String sn, String mail) {
        super();
        this.name = name;
        this.sn = sn;
        this.mail = mail;
    }

    @Relationship(type="REPORT_TO", direction=Relationship.OUTGOING)
    private Employee manager;

    @Relationship(type="REPORT_TO", direction=Relationship.INCOMING)
    private Set<Employee> staffs = new HashSet<>();

    ...
}

我使用REST API作为我的应用程序的界面。当我尝试使用session.loadAll(Employee.class, 0)返回所有员工时,似乎一切正常,但不会显示经理或员工信息。另一方面,如果我使用session.loadAll(Employee.class, 0),则会返回许多深度的Employee。

例如,如果员工A向员工B报告,即。 B是A的管理者。深度0的结果是:

{ "_embedded" : { "employees" : [ { "name" : "B", "sn" : "10001", "mail" : "B@test.com", "manager" : null, "staffs" : [ ], }, { "name" : "A", "sn" : "10000", "mail" : "A@test.com", "manager" : null, "staffs" : [ ], } ] } ... }

而深度1的结果是: [{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com","manager":{"id":8,"name":"B","sn":"10001","mail":"B@test.com","manager":null,"staffs":[{"id":9,"name":"A","sn":"10000","mail":"A@test.com",...

我该如何处理此案?请问有人给我一些建议。

1 个答案:

答案 0 :(得分:0)

这是一个JSON序列化问题。实体包含循环引用,并且序列化程序无法检测它们,因此它进入构造JSON对象的无限循环。您可以尝试使用JSOG Jackson插件,该插件能够检测并自动处理此类问题。有关如何在Spring项目中使用它的示例,请查看SDN University