Skip to content

Provide saveAs overloads with a predicate for selecting projected attributes. #2454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

michael-simons
Copy link
Collaborator

This implements #2420. Please @Andy2003 check if this suites your needs.

Before you ask: Chances are minimal to have something similar for reading. For read it just feels like the wrong approach. If we are down there, one might ask for the relevance of the mapping process itself and just delegate to the client.

Thanks @meistermeier for your help on this. I did not touch anything that might be affected by spring-projects/spring-data-commons#2420

Funny coincidence that those things have the same issue number :D

BiPredicate<PropertyPath, Neo4jPersistentProperty> predicate,
BiConsumer<PropertyPath, Neo4jPersistentProperty> sink
) {
this.pathsTraversed.clear();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this call and the current usage looks like new PropertyTraverser(..).traverse(..) wouldn't it make sense to make those methods static?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. The thing has state.

* @return the saved instance.
* @since 6.3
*/
default <T> T saveAs(T instance, BiPredicate<PropertyPath, Neo4jPersistentProperty> includeProperty) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we accept it, the instance should be @Nullable. Would also make sense for the saveAllAs method and the reactive counter-parts. (Would need an additional null check in the saveAllAs/saveAllImpl).

@Andy2003
Copy link
Contributor

Andy2003 commented Dec 16, 2021

Given

Node node2 = Node.builder()
		.name("test123")
		.parent(rootNode) // rootNode was queried before
		.build();

neo4jTemplate.saveAs(node2, (propertyPath, property) -> {
	if (propertyPath.stream().count() > 1){
		// do not save deeper than level 1
		return false;
	}
	if (!property.isRelationship()) {
		return true;
	}
	return property.getName().equals("parent");
});

3 write queries are triggered:

  1. the merge of the newly created node including its properties to save RUN "MERGE (node:Node {nodeId: $__id__}) SET node += $__properties__ RETURN node" {__id__="5dca7a8c-8f5e-4089-aae5-5e794332b7c0", __properties__={name: "test123", nodeId: "5dca7a8c-8f5e-4089-aae5-5e794332b7c0", created: NULL}}
  2. a non required update of the root node MERGE (node:Node {nodeId: $__id__}) SET node += $__properties__ RETURN node" {__id__="root", __properties__={}}
  3. The creation of the relation: MATCH (startNode:Node) WHERE startNode.nodeId = $fromId MATCH (endNode) WHERE id(endNode) = $toId MERGE (startNode)-[relProps:CHILD_OF]->(endNode) RETURN id(relProps)" {toId=40, __knownRelationShipId__=NULL, fromId="5dca7a8c-8f5e-4089-aae5-5e794332b7c0"}

The 2nd query should be filtered out

@meistermeier
Copy link
Collaborator

Sorry for the late reply. Could you please describe why you explicitly add parent to the list but on the other hand say SDN should not persist it.
I agree, on a technical level saving a node without properties does not make any sense at first sight. But this needs also be done if e.g. the root node is a new node.

@Andy2003
Copy link
Contributor

Andy2003 commented Jan 3, 2022

With parent I meant that the relation should be stored. But I don`t want to change any properties at the parents node.

@meistermeier
Copy link
Collaborator

I would like to keep this out of this PR.
I am not yet convinced that we would ever make a difference between the relationship and the relationship with a related node in the future.

@meistermeier
Copy link
Collaborator

Merged with f79ad5393a9f9a54ec800f00a71b3a87a8c257e4

@meistermeier meistermeier deleted the issue/2420 branch January 4, 2022 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants