实体

时间:2017-10-09 05:06:15

标签: neo4j spring-data-neo4j spring-data-neo4j-4

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

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

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();

1 个答案:

答案 0 :(得分:1)

您必须在Company中声明(通过EmployeeEmployeeRel的传出关系)

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

Employee中的倒数:

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

请注意,您在这里选择了双方都可以导航的关系,但这不是强制性的。它也可以让它仅从CompanyEmployee导航。