-
Notifications
You must be signed in to change notification settings - Fork 618
Description
currently the neo4jTemplate.save
method saves the whole sub graph provided. This makes it hard to just create or update a single node which has relations to other nodes.
Request:
Adding a method e.g. neo4jTemplate.saveJust(node)
which will create/update just the provided node and the relations to other nodes. Instead of also creating related nodes it will expect the related nodes to already exist and fail if this it not the case.
So basically the same logic as the current save
method explained in the docs: Appendix -> Query Creation but with a modified step 3 and without step 5.
Current solution:
My current solution is to use projections for the node classes I want to save. E.g. I have a User-Node and Post-Node and when adding a new Post I want to link it with the user who posted the post:
public class User {
@Id @GeneratedValue
String id;
String name;
String email;
@Relationship(type = "FOLLOWER" direction = Relationship.Direction.INCOMMING)
Set<User> follower;
}
public class Post {
@Id @GeneratedValue
String id;
String title;
String description;
@Relationship(type = "POSTED_BY" direction = Relationship.Direction.OUTGOING)
User user;
}
My projections look like this:
public interface PostProjection extends IdProjection {
String getTitle();
String getDescription();
String getImageUrl();
IdProjection getUser();
}
public interface IdProjection {
String getId();
}
When using neo4jTemplate.saveAs(node, PostProjection.class)
I get the expected result that just the post and relation to the user is created without the user being modified.
Motivation:
I think that creating/updating a single node is a general use case and with the current solution I have to create projection interface for almost every node-class which exactly following the same schema:
Every projection interface contains getter for all properties, plus getters with IdProjection
for all relationships.
From my point of view this is very much boilerplate code and makes the project harder to maintain as adding new fields requires updating the node class and the projection class.