spring 实体中的数据 neo4j 关系
spring data neo4j relationship in entity
图片我有以下实体:公司和员工,带有 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<>();
或
@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();
或
@Relationship(type = "EMPLOY", direction = Relationship.OUTGOING)
private EmployRel employRel = new EmployRel();
您必须在 Company
中声明(通过 EmployeeRel
与 Employee
的传出关系)
@Relationship
public Set<EmployRel> employees = new HashSet<>();
Employee
中的逆运算:
@Relationship(direction = Relationship.INCOMING)
public HashSet<EmployRel> isEmployedBy = new HashSet<>();
请注意,您在这里选择了让关系在两侧都可导航,但这不是强制性的。它也可以让它只能从 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<>();
或
@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();
或
@Relationship(type = "EMPLOY", direction = Relationship.OUTGOING)
private EmployRel employRel = new EmployRel();
您必须在 Company
中声明(通过 EmployeeRel
与 Employee
的传出关系)
@Relationship
public Set<EmployRel> employees = new HashSet<>();
Employee
中的逆运算:
@Relationship(direction = Relationship.INCOMING)
public HashSet<EmployRel> isEmployedBy = new HashSet<>();
请注意,您在这里选择了让关系在两侧都可导航,但这不是强制性的。它也可以让它只能从 Company
或 Employee
.