spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
914 stars 560 forks source link

JSON Patch to add/replace child entity collection fails [DATAREST-996] #1359

Open spring-projects-issues opened 7 years ago

spring-projects-issues commented 7 years ago

Tommy Ludwig opened DATAREST-996 and commented

The sample project has an integration test to reproduce this. The AggregateChildUpdateSampleApplicationTests.canReplaceCommentsWithPatch test demonstrates the issue and results in the following exception.

Essentially, when trying to update a collection of child entities that belongs to an aggregate root (child entities do not have their own repositories) with JSON Patch, Spring Data REST since Hopper (it seems) gives an error. With Gosling (Spring Boot 1.3.x), this does work, though. Notably different is the upgrade from Hibernate 4 to 5, which may be related.

Caused by: org.springframework.data.rest.webmvc.json.patch.PatchException: Could not read [{"content":"Cool post!"}] into class org.hibernate.collection.internal.PersistentSet!
    at org.springframework.data.rest.webmvc.json.patch.JsonLateObjectEvaluator.evaluate(JsonLateObjectEvaluator.java:47) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.json.patch.PatchOperation.evaluateValueFromTarget(PatchOperation.java:177) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.json.patch.AddOperation.perform(AddOperation.java:43) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.json.patch.Patch.apply(Patch.java:64) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.config.JsonPatchHandler.applyPatch(JsonPatchHandler.java:91) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.config.JsonPatchHandler.apply(JsonPatchHandler.java:83) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.readPatch(PersistentEntityResourceHandlerMethodArgumentResolver.java:198) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    ... 53 common frames omitted
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:143) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:308) ~[jackson-databind-2.8.6.jar:2.8.6]
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:259) ~[jackson-databind-2.8.6.jar:2.8.6]
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26) ~[jackson-databind-2.8.6.jar:2.8.6]
    at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3770) ~[jackson-databind-2.8.6.jar:2.8.6]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2099) ~[jackson-databind-2.8.6.jar:2.8.6]
    at org.springframework.data.rest.webmvc.json.patch.JsonLateObjectEvaluator.evaluate(JsonLateObjectEvaluator.java:45) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
    ... 59 common frames omitted

Affects: 2.6 GA (Ingalls), 2.5.7 (Hopper SR7)

Reference URL: https://github.com/shakuzen/aggregate-child-update-sample

4 votes, 7 watchers

dcardozo commented 3 years ago

Is there a workaround to this issue that doesn't involve downgrading SDR or Hibernate?