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;
    ......
}

那么如何在 CompanyPerson 中保留引用?

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 中声明(通过 EmployeeRelEmployee 的传出关系)

@Relationship
public Set<EmployRel> employees = new HashSet<>();

Employee 中的逆运算:

@Relationship(direction = Relationship.INCOMING)
public HashSet<EmployRel> isEmployedBy = new HashSet<>();

请注意,您在这里选择了让关系在两侧都可导航,但这不是强制性的。它也可以让它只能从 CompanyEmployee.

导航