Search code examples
javaneo4j-ogm

How to add object in a list of object in ne04j


I have 2 classes as following

class Organization{
    @GraphId
    private Long Id;

    @Relationship(type = "has", direction="Outgoing)
    private List<Person> personList;
}

and

class Person{
    @GraphId
    private Long Id;
    private String name;  
}

and organization repo

interface OrganizationRepository extends GraphRepository<Organization> {}

I can Create organization and add list of person to it.

Now How can i add a new person to existing organization?

I can get all the person of a organization in a list and then add the new person to that list and then update the organization. but this does not seems the right way to do it.

So is there any other way to add a person to existing organization??


Solution

  • That's right, load the organization, add the person to the list, and save the organization.

    Edit based on comments:

    If the organization can contain 50,000 persons, then perhaps this is not the right model for you. How about a reference to the organization on the Person instead? Then, all you need to do is load the person, set his organization, and save the person.