开发者

LazyInitializationException in Spring

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开发者_C百科 the data stored in a Object linked to the user?

Thanks


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜