I defined repository for parent entity, and custom controller with @RepositoryRestController, the GET method will return a list of parent entity. When tests with the Postman, the GET method return parent entity and its children entities with full JSON output. I am expecting the error like:
"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: failed to lazily initialize a collection of role: aero.stellar.rockstar.rules.model.entities.SchedulingRule.conditions, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: aero.stellar.rockstar.rules.model.entities.SchedulingRule.conditions, could not initialize proxy - no Session
Instead, it did not have this error, it returns the full parent/children JSON.
If i change my controller annotation to @RestController, then i will have above error, which is what i expect.
Can someone give insights why @RepositoryRestController treat LAZY fetch type differently than @RestController? I did not find any Spring documentation mentioning this side effect
That's working as expected. The related entities are not eagerly loaded but on demand when Jackson traverses the properties. The difference to a simple @RestController is that Spring Data REST automatically registers an OpenEntityManagerInViewInterceptor for the controllers annotated with @RepositoryRestController to make sure the controllers can access properties that might have been declared as lazy if the framework needs to. An example of that are properties, which we expose association resources for. We need to access those to be able to list and manipulate them and without that interceptor in place we'd not be able to implement those with JPA as a backing store.
Can you elaborate why you'd like to the see the exception rather than a properly rendered representation? Remember, Spring Data REST is not supposed to know anything about store specifics in the first place. A lazy initialization exception is effectively the data store leaking into the UI. I.e. the application of the OEMIVI makes the class just work as it's supposed to work: returning a list, when calling the getter for that list.
In any case I am going to tweak the JavaDoc for @RepositoryRestController and leave a note in the documentation on when to use the annotation (in contrast to @BasePathAwareController)
Yongqin Xu opened DATAREST-1381 and commented
I have a parent(rule) and children entity(conditions) with one to many relationship using LAZY fetch type.
@OneToMany
(mappedBy = "rule", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)I defined repository for parent entity, and custom controller with
@RepositoryRestController
, the GET method will return a list of parent entity. When tests with the Postman, the GET method return parent entity and its children entities with full JSON output. I am expecting the error like:"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: failed to lazily initialize a collection of role: aero.stellar.rockstar.rules.model.entities.SchedulingRule.conditions, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: aero.stellar.rockstar.rules.model.entities.SchedulingRule.conditions, could not initialize proxy - no Session
Instead, it did not have this error, it returns the full parent/children JSON.
If i change my controller annotation to
@RestController
, then i will have above error, which is what i expect.Can someone give insights why
@RepositoryRestController
treat LAZY fetch type differently than@RestController
? I did not find any Spring documentation mentioning this side effectAffects: 3.1.8 (Lovelace SR8)
Referenced from: commits cf18d6a, f3b1d92, d80070a, 8b9ed0c
Backported to: 3.1.9 (Lovelace SR9)
The text was updated successfully, but these errors were encountered: