Skip to content
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

Neo4JRepoistory.save does not correctly updates @RelationshipEntity #607

Closed
dermoritz opened this issue Feb 20, 2019 · 2 comments · Fixed by #719
Closed

Neo4JRepoistory.save does not correctly updates @RelationshipEntity #607

dermoritz opened this issue Feb 20, 2019 · 2 comments · Fixed by #719
Assignees

Comments

@dermoritz
Copy link

I created an repo to demonstrate the issue (unit test fails):
https://github.com/dermoritz/neo4JSpringExamples

Test: https://github.com/dermoritz/neo4JSpringExamples/blob/master/src/test/java/com/example/neo4jbug/RoleRpositoryTest.java

There is a problem with calling Neo4JRepository.save to update a @RelationshipEntity.
The test creates a Movie and an Actor and puts them in realtion "Role" (this is an example from doc)

the first save persists the relation and nodes correctly - i check this by retireving the entity from repository.
Then i create a 2nd movie and set this as node in the former relation and save it.
While the save returns a correctly updated entity the persisted state in data base is different: The second movie is persisted but the relation is still between the actor and first movie:
image

My expectation is that this should just work: The entity returned by save should be reflect the new state in database - node correctly updated.

If it is not possible (by design) to update a relation's node either the save method should return an error.

The current state is really misleading and is contradicting to expectations or the "contract" of the spring data repository save method.

What does not help:

  • give the Actor and/or Movie a property that contains the role (as suggested in documentation)
  • to play around with transactions, an find or something similar should flush the state but in fact in this case the new relation is never persisted.
@meistermeier meistermeier self-assigned this Feb 21, 2019
@meistermeier
Copy link
Collaborator

It looks like this is related to the missing

@Relationship(type = "PLAYED_IN")
private Set<Role> roles;

or for 1:1

@Relationship(type = "PLAYED_IN")
private Role roles;

definition in Actor. Setting this in your Actor class should fix the problem.
What I also noticed in the example: You are not using any explicit transaction boundaries. This can lead to problems with caching similar to what you currently see.
Basically rule of thumb is: load, modify and save should happen in one transaction.

@dermoritz
Copy link
Author

I updated the code.

As stated before adding the relation to the node does not change anything.
But at least adding @transactional to test method changed something (the former test runs green):
image

So at least there is a new relation and also the return of save reflects this correctly (it returns a new id).

But this does not changes the core problem (the title of this ticket):

  • save on an existing entity should update it or throw an error ("update not supported on relations")
  • save on non existing entity is working fine - i don't need to explicitly set transaction (layer handles it). The update should behave the same way - because it is the same function.

Propbably a Relation repository would provide different methods than Neo4JRepository?!

michael-simons added a commit that referenced this issue Dec 10, 2019
This provides example code and proof that the point ends of relationships can be updated. The update must happen in one transaction, together with the loading of the relationship.

See related commit spring-projects/spring-data-neo4j@bbf7ec1 in Spring Data Neo4j.

This closes #607.
michael-simons added a commit that referenced this issue Dec 11, 2019
This provides example code and proof that the point ends of relationships can be updated. The update must happen in one transaction, together with the loading of the relationship.

See related commit spring-projects/spring-data-neo4j@bbf7ec1 in Spring Data Neo4j.

This closes #607.
michael-simons added a commit that referenced this issue Dec 11, 2019
This provides example code and proof that the point ends of relationships can be updated. The update must happen in one transaction, together with the loading of the relationship.

See related commit spring-projects/spring-data-neo4j@bbf7ec1 in Spring Data Neo4j.

This closes #607.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants