Warm tip: This article is reproduced from serverfault.com, please click

Duplicate instances for same entity in Neo4j OGM

发布于 2021-06-15 13:32:57

I am having problems upgrading from Spring Boot 2.0.8 to 2.3.11 / Spring Data Neo4j 5.0.13 to 5.3.9.

I have a REST API with CRUD operations for my entities, and use Jackson ObjectIdResolvers to resolve related entities from id's in the incoming JSON request. The entities have a custom id field of type UUID, and the resolvers use Neo4j repositories to lookup the entities by the UUID key.

In my case, 3 entities are related:

(A)--(B)--(C)

A has a list of B, and B has a list of C. POST to create a new C:

{
  "name": "My C",
  "b": "c3c9ec23-ff05-4295-9b5b-209af08328ac"
}

The resolver for B will lookup the entity and populate my instance of C. If I do a specific findById for B within the resource, I get the same instance as I had from the resolver - which is exactly what I want.

Now, when upgrading to Spring Data 5.3.9, I get a different result. A new lookup in the rest controller will return a second instance representing the same entity that was loaded by the resolver - with the same ID and UUID. Seems like the instance loaded by the resolver is not in the mapping context. This causes problems in the save since C references an instance that is not in the context, and typically it causes relationships to be deleted.

Specifically, the instance B loaded by the resolver has an instance of A, but A is not fully loaded (the list of B only contains one single instance, the others are not loaded). A lookup of A in the rest controller will return an instance with fully loaded the list of B. Saving the new C with the B reference from the resolver will cause relationships between A and B to be deleted.

How can I get the instance from the resolver into the mapping context? Or am I doing something fundamentally wrong here?

Questioner
Mattias O
Viewed
0
Mattias O 2021-08-27 22:42:43

The problem was resolved by explicitly setting spring.data.neo4j.open-in-view: true in application.yaml, since the default has been changed.

See https://github.com/neo4j/neo4j-ogm/issues/881 for more details.