-->
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.  [ 6 posts ] 
Author Message
 Post subject: Lazy initialization problem
PostPosted: Mon Jan 19, 2004 11:17 am 
Beginner
Beginner

Joined: Fri Nov 28, 2003 6:57 am
Posts: 20
Hi,

I've a business object which contains a set and this set is configured as lazy loading. My test class looks like this (pseudo code):

public class Testclass {
protected Session session = null;
protected Iterator iterator;

Testclass {
// initialize session and iterator; the iterator iterates over my business object
}

public static void main() {
// use the iterator to iterate over the BOs...
int children = businessObject.getSet.size(); // this call fails!
}
}

I get the following Exception: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session

I never closed the session (the documentation tells me not to do it). Can somebody tell me where the problem is? Could it be a problem that the method is static and the session not? Or that the session is "blocked" by the iterator?

Thanks,

Thorsten


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 11:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please show your real code.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 11:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The session has been closed. Show a simplified but real code.
The undeplying collectino keep a session reference to lazy load.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 11:42 am 
Beginner
Beginner

Joined: Fri Nov 28, 2003 6:57 am
Posts: 20
Here's the code:

Code:
public class ManualMatcherFacade {
private static final String QUERY = ...;
protected Session session = null;
protected Iterator iterator;

public ManualMatcherFacade() {
  try {
   session = PersistenceLayer.getInstance().getSession();
   iterator = EntityManager.genericIterator(session, QUERY);
  } catch (DataException e) {}
}
   
public static void main(String[] args) throws Exception {
  ManualMatcherFacade m = new ManualMatcherFacade();
  for (int i=0; i<10; i++) {
   // for each possible vendor
   Object[] o = (Object[]) m.next();
   Integer key = (Integer) o[0];
   NormalizedVendor vendor = NormalizedVendorManager.findByPrimaryKey(key);
   System.out.println("Size: " + vendor.getComparisonResults().size());
}
}


And that's the mapping of the set in NormalizedVendor:
Code:
<set name="comparisonResults" table="ETL2_MAP_VENDOR_CR" lazy="true">
<key>
  <column name="VENDOR" not-null="true"/>
</key>
<many-to-many class="com.degussa.spr.dw.common.data.ComparisonResult">
  <column name="RESULT" not-null="true"/>
</many-to-many>
</set>

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 11:58 am 
Beginner
Beginner

Joined: Fri Nov 28, 2003 6:57 am
Posts: 20
I just debugged the code and found the problem: the method to retrieve the vendor closed the session. The set has this session instead of the session defined in my class. I think I have too change my design a little bit...

By the way: does anybody know when the Hibernate book will be available? I'd appreciate a good book and the chapters I read are very good.

Bye,

Thorsten


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2004 12:01 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.manning.com/bauer/

We are currently finishing the book, it will be available in the 2nd quarter 2004. I'm pushing to get some chapters earlier in the Manning Early Access Program (you pay for a subscription to the draft chapters as they go to production and receive a print copy when it is available), but don't expect anything before early March.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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

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.