0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So how about using UNDIRECTED in the direction?
UNDIRECTED is a matter of semantics. Let's say you have a PARTNER relationship : you don't really matter the direction, so you can use UNDIRECTED in that case. It will work the same as a directed relationship.
Thanks, I know this direction semantics, for my case, maybe you need to read my another question indeed, please see - stackoverflow.com/questions/46503104/…, in this question, the company can be the "Investor" and "Investee", so will have the Company -[:INVESTMENT]- Company relationship, this is really the boring things to me.
I posted a comment there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.