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...
Ok sorry for the wild guessing in the last mails, I just read up
the spring docs, and I now have a clearer picture of things. I think I
can point you towards a definitive solution now.
The lazy loading and the old objects BINDING approach you follow is
rather unnecessary if you use a datamodel instead of feeding old objects
into the session (probably via a collection you have stored somewhere in
the session scope).
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?
Thanks in advance. I'm looking forward to hearing your comments.
Enrique Medina.
O think I know where your problems is, you try to recycle the old
objects in a new session which is opened automatically.
The objects are probably stored in a session scoped collection
and then fed back into the model after the request (which is caused
by the paging), theoretically you could use bind to lock them to the new
session, but that is problematic and not really necessary.
The best way to solve your problem if you dont want to go the shale
route (which basically does the same as the Spring class in a safer way)
is, just to implement a pure datamodel,
you get the session from spring anyway, and feed the data from the
methods you have to implement from the model, just as you guessed.
The call apprach probably will go like that
Constructor of the model... request the session from spring, which is
already opened
The next callbacks are for delivering the number of rows etc...
then load the object row by row as the outer system requests it from the
model and you are set...
For performance increase you probably can
set an iterator on the give me the number of rows methods
and then recycle that one for each subsequent, give me the next row
request. That way it should work.
Sorry for the guessing from my side in the last mails. I overlooked the
Spring part.
Either way, you probably will have to implement a specialized datamodel.