I have a self relations (a staff can report to a manager and a manager may have many staffs)as follows:
@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<>();
...
}
I use REST API as the interface of my application. When I try to return all Employee with session.loadAll(Employee.class, 0)
, it seems everything fine but no manager or staff info will show. On the other hand, if I use session.loadAll(Employee.class, 0)
, to many depths of Employee are returned.
For example, if employee A reports to employee B, viz. B is the manager of A. The result for depth 0 is :
{
"_embedded" : {
"employees" : [ {
"name" : "B",
"sn" : "10001",
"mail" : "B@test.com",
"manager" : null,
"staffs" : [ ],
}, {
"name" : "A",
"sn" : "10000",
"mail" : "A@test.com",
"manager" : null,
"staffs" : [ ],
} ]
}
...
}
while the result for depth 1 is :
[{"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",...
How can I handle the case? Could anyone give me some advices, please.
This is a JSON serialisation issue. The entities contains circular references, and the serialiser is unable to detect them, so it enters an infinite loop constructing the JSON objects. You could try the JSOG Jackson plugin, which is able to detect and automatically handle this kind of problem. For an example of how it can be used in a Spring project, have a look at SDN University.