LazyInitializationException in Spring
I am getting LazyInitializationException in my Spring application. I have OpenEntityManagerInViewFilter set up so I have all my relationships set as FetchType.LAZY and they all work. The problem is when I try to access a user who is in a session via Spring Security and print the CLIMM information to a JSP, something like this:
<sec:authentication property="principal" var="userAuth"/>
${userAuth.organisation.id}
and i get
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
although I can execute $ {userAuth.username} without problem
I have my own Authentication Provider which is DAO only which loads users using Hibernate
<sec:authentication-provider user-service-ref="userDAOImpl">
The weird thing is that I get the same problem when I set the Organization object to EAGER.
The problem only occurs when trying to access via sec: authentication. If I add a user to the model and then access the data in the jsp, it works.
Why can't I access the data stored in the object associated with the user?
thanks
a source to share
As you got the basic information when the user logged in and saved it in the http session at that moment.
If you try to access an uninitialized collection of that object on any subsequent HTTP request, the hibernate session (where the main one was selected) is closed (immediately after login) and you get a LazyInitializationException
a source to share