-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
LazyInitializationException on REST services #67
Comments
This is "normal" with JPA, if you access a lazy relationship outside of a transaction. Normally, you should use a service layer, with a transaction. For instance, the UserService class is perfect for doing this job. Now, I understand this is a pitfall with JPA, so we should work on two possible solutions (outside of documenting the issue):
|
As I feared we can't use an extended persistence context with our stack. So the only two options we have left is:
|
Agree with you, I don't like open session view pattern coz some exceptions (db exceptions) are only detected when the transaction is committed which happens outside of the controller. What about to provide an option during the generation of the app where the developer can automatically include or not a open session in view filter? Thanks, |
This could be a solution: I don't want to have too many options when generating the app, but some options come and go depending on how we evolve. For me this is not a very high priority option, but I'll see that in a couple of versions (I think we will lose some options soon) |
In fact, this is a pitfall with Hibernate, not JPA. |
Hi All,
Checking the code, I realised that REST services can throw LazyInitializationException when the code is trying to access a relationship. The transaction has been completed but the code is accessing a lazy relationship which requires a transaction.
To replicate the issue, in web.rest.AccountResource.getAccount(HttpServletResponse) I added the following line:
log.debug("User authorities size: " + user.getAuthorities().size());
Then, I logged in the app and in the log I saw the following exception:
[ERROR] ar.com.tunuyan.conf.DispatcherServletConfiguration - An error has occured: failed to lazily initialize a collection of role: ar.com.tunuyan.domain.User.authorities, could not initialize proxy - no Session
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ar.com.tunuyan.domain.User.authorities, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:156)
at ar.com.tunuyan.web.rest.AccountResource.getAccount(AccountResource.java:53)
The text was updated successfully, but these errors were encountered: