-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Clarification needed : OpenSessionInView + Lazy Loading
PostPosted: Mon Sep 27, 2004 8:52 am 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
Hibernate version:
2.1.6 (and Struts 1.1, Spring 1.1)

Name and version of the database you are using:
mySQL

Hi There,
I have been trying the Spring OpenSessionInView and Lazy loading in the past few days. Would like to seek clarification from the experts here. I have tried Google the anwser but could not a clear yes or no.

I think the problem I am facing is common as I can see quite a few posting in tis forum and Spring's forum:

- I get "net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed" when I tried to access a class B (which is a Set) in class A. A has one-to-many association with B.
I get A first and then pass to View layer. In another request, I try to get A.getBs(), then this is where I got the error message.
I trace the problem and found that B is bound with a 'initialize=false' session which was created by the OpenSessionInView in the previous request.
So it does not work even though OpenSessionInView has created a new Hibernate session in the second request.

Here are my questions:

1. Does it mean that 'OpenSessionInView' plus 'lazy=true' configuration is not enough for the above use case ? I think this use case is common and I can not find clear yes or no anwser in docs explaining OpenSessionView and/or 'lazy=true'.

2. If, I have to deal with this, what's the best practice ?
2A) there is a suggestion to have some code like this:
-------
Session session = SessionFactoryUtils.getSession((SessionFactory)getBean("canalSessionFactory"), false);
session.lock(t, LockMode.NONE);
-------

I can have this in a BaseAction() so whenever needed, I can check this. However, it means View layer now needs to understand my Persistence configuration, which is not a good thing to do.
I can move this to service or lower layer but it still means that my View (struts actions for example) have to explicitly call this.

2B) Or:
--------
if (!Hibernate.isInitialized (a.getBs()))
Hibernate.initialize (a.getBs());
--------

I can have this in the ADAOHibernate class, wheneven it is asked to get A, always run this code to make sure that subsequent getBs() at any point will be successful.
This solution seems cleaner because Struts action does not need to do anything.
The tradeoff of this is that the initialization might be wasted if View does not need to getBs().

Another situation is that, if we have this association:
A has one-to-many with B, B has one-to-many with C

I may have a page displaying all A I have; then goes to next page which displays details of a selected B - I MUST call getA(id) becuase Bs are not initialized though A was already retrieved. Similarly, I have to call getB(id) again if there is page to display the details of selected B - becuase Cs for this B is not initialized.

Really appreciate your advice !

thanks !
li xin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.