2

I'm getting a LazyInitializationException in my Spring Application. I have the OpenEntityManagerInViewFilter configured so I have all my relations set as FetchType.LAZY and they all work. The problem is when I try to access the user which is in session via Spring Security and print LAZY information in the 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

though I can do ${userAuth.username} without any problem

I have a custom authentication provider which is just a DAO 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 organisation Object to EAGER.

The problem only happens when I try to access through sec:authentication. If I add the user to the model and then access to the data in the jsp it works.

Why can't I access to the data stored in a Object linked to the user?

Thanks

1
  • did you search stackoverflow for this exception? Commented Apr 29, 2010 at 12:16

1 Answer 1

6

Because you fetched the principal information at user login, and stored them in the http session at that moment.

If you try to access a non initialized collection of that object at any later http request, the hibernate session (where the principal was fetched) is closed (just after the login), and you get the LazyInitializationException

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.