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

Improve documentation on the effects of @RepositoryRestController [DATAREST-1381] #1741

Closed
spring-projects-issues opened this issue May 22, 2019 · 2 comments
Assignees
Labels

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented May 22, 2019

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 effect


Affects: 3.1.8 (Lovelace SR8)

Referenced from: commits cf18d6a, f3b1d92, d80070a, 8b9ed0c

Backported to: 3.1.9 (Lovelace SR9)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 4, 2019

Oliver Drotbohm commented

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)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 5, 2019

Oliver Drotbohm commented

That's in place now. Still happy to augment after further feedback

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

No branches or pull requests

2 participants