The problem is that the bean that holds the list of data for the table
is session scoped. So, on the first request to the page the hibernate
query is done and then you store the resulting List in the HTTP
session. On a subsequent request to the page you try to access data in
the List that you didn't access on a previous request. Since the List
is lazily loaded and you are no longer operating within the same
Hibernate session that was used to do the query you get the lazy
initialization exception.
A general rule of thumb that I've had to adopt is to never put a
Hibernate returned object or List in the HTTP session because if there
is any lazy loading involved, whether they're elements in a List or
properties of an object, you will get the LLE. It's better just to
re-run the query. If you're using the 2nd level cache and query caching
then it's not that much more overhead and much less work than trying to
do something like DTOs.
Rich
Ryan Wynn wrote:
Any hibernate-spring experts out there that could tell me how I can
still get a LazyInit exception inside my spring
OpenSessionInViewFilter. My stack trace clearly says I am inside the
Filter. Exception - No session or session was closed.
This is happening when using the myfaces datascroller for a datatable
and paging. Bean that holds List of data for table is session scoped.
Unfortunately, the data is a list of hibernate objects. I probably
should be copying these into plain pojos for the view, but that is not
what I am doing. Probably because then I could need to replicate my
entire object tree.
- Re: Hibernate and Datascroller Richard Wallace
-