Saving node without updating node in relationship?


(Toulouse Sabrina) #1

Hi, I'm new to Neo4J

In my app I have those entities :

 @NodeEntity
 public class ManagedList {
     @Id
     @GeneratedValue()
     private Long id;
 
     private String name;
 
     @Relationship(type = "CONTAINS")
     private List<ListElement> listElement;
 
}
 
 @NodeEntity
 public class ListElement {
     @Id
     @GeneratedValue()
     private Long id;
 
     private int elementOrder;
 
     @Relationship(type = "CONTAINS", direction = Relationship.INCOMING)
     private ManagedList managedList;
 }

When I add a new ListElement to a ManagedList with my WS, the ListElement in the JSON only has the ManagedList id, not the full object (to avoid sending unecessary informations). So when this JSON is mapped into a ListElement entity, nested ManagedList object only has its id, the other properties like name are == null.

Previously my app used to work with Mysql, and when I saved ListElement this way, it was correctly associated with ManagedList, and ManagedList object still has his name in DB (ManagedList object was not updated in any way in DB when I saved ListElement , no matter what has changed in its properties).

But now with Neo4J DB, when I save ListElement, it also updates the ManagedList object and set the name to null.

Is there any way to specify that when I save ListElement I don't want to update ManagedList, but only use ManagedList id to associate them, like it uses to happen in Mysql? Or this is impossible and I must first get the ManagedList object with the id, set it into my ListElement, and then save?

Many thx


(Thomas Shields) #2

I recommend reading this relevant section of the OGM docs: https://neo4j.com/docs/ogm-manual/current/reference/#reference:session:persisting-entities

Short version: I don't think there's a way to exactly accomplish what you want, but the OGM session is smart enough to know that you haven't changed the ManagedList, so if you can load the full ManagedList once and attach it to the ListElements, saving new ListElements will not re-save ManagedList's properties (as far as I understand it)