Enrique Medina wrote:
Hi,
Maybe this is not the most adequate forum to post this question, but I
know many of you are using MyFaces together with Spring & Hibernate,
so it would be great it you give me some comments.
The problem I have comes with Lazy collections in my objects. Assuming
you know how to use Hibernate & Spring, the problem comes when I try
to paginate my DataTable using the DataScroller, because the
OpenSessionInViewFilter pattern establishes the session Hibernate in
the initial query (the one that shows the first page of the
DataTable). So, when the following page is needed, as MyFaces executes
a new request, the session binded by the OpenSessionInViewFilter for
this new request, is not obviously the same as the session previously
established at the first request.
Finally, I always get a Lazy initialization exception from Hibernate,
as you surely have imagined...
So my question is very simple:
How do you manage objects with lazy collections being shown in
different request with respect to the OpenSessionInViewFilter pattern?
Simple answer, to a relatively deep question. You cannot. To be more
precise, there is a huge flaw in the access pattern of the Datamodel,
which is somewhat fixed in shale (Clanahan, the core maintainer of Shale
pointed me towards it), and that is the way you can get access
boundaries in the Datamodel, which is somewhat non existent.
But back to your problem which has more to do with Hibernate than with
the half broken Datamodel.
The easiest way to deal with your problem is relatively simple, avoid
the lazy loading for the parts you want to display in the data table
and only use it for the parts you dont want to display, to avoid
excessive data loading.
You can do that either by tinkering with the Hibernate config params, or
by accessing the lazily loaded parts during session time once.
Depending on your data structures one approach or the other might
be more efficient.