Image我有以下实体:Company和Employee,带有spring数据neo4j注释:
Company.java
@NodeEntity(label = "Company")
public class Company {
/**
* Graph ID
*/
@GraphId
private Long id;
......
}
Employee.java
@NodeEntity(label = "Employee")
public class Employee {
/**
* Graph ID
*/
@GraphId
private Long id;
......
}
然后是这些实体的关系实体:
@RelationshipEntity(type = "EMPLOY")
public class EmployRel {
/**
* Graph ID
*/
@GraphId
private Long id;
@StartNode
private Company company;
@EndNode
private Employee employee;
......
}
那么如何在Company
和Person
中保留引用?
Company.java
@Relationship(type = "EMPLOY", direction = Relationship.OUTGOING)
private Set<EmployRel> employeeRel = new HashSet<>();
OR
@Relationship(type = "EMPLOY", direction = Relationship.OUTGOING)
private Set<Employee> employee = new HashSet<>();
Person.java
@Relationship(type = "EMPLOY", direction = Relationship.INCOMING)
private Company company = new Company();
OR
@Relationship(type = "EMPLOY", direction = Relationship.OUTGOING)
private EmployRel employRel = new EmployRel();
答案 0 :(得分:1)
您必须在Company
中声明(通过Employee
与EmployeeRel
的传出关系)
@Relationship
public Set<EmployRel> employees = new HashSet<>();
Employee
中的倒数:
@Relationship(direction = Relationship.INCOMING)
public HashSet<EmployRel> isEmployedBy = new HashSet<>();
请注意,您在这里选择了双方都可以导航的关系,但这不是强制性的。它也可以让它仅从Company
或Employee
导航。