0
votes

Image I have the following entity: Company and Employee, with spring data neo4j annotation:

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

Then there is the relationship entity for these entities:

@RelationshipEntity(type = "EMPLOY")
public class EmployRel {
    /**
     * Graph ID
     */
    @GraphId
    private Long id;
    @StartNode
    private Company company;
    @EndNode
    private Employee employee;
    ......
}

So how to keep the reference in Company and 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();
1

1 Answers

1
votes

You have to declare in Company (outgoing relationship to Employee through EmployeeRel)

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

And the inverse in Employee :

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

Note that here you chose to have the relationship navigable on both sides, but it is not mandatory. It will also work to have it navigable only from Company or Employee.