-->
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.  [ 3 posts ] 
Author Message
 Post subject: ...failed to lazily initialize a collection of role...
PostPosted: Tue Aug 09, 2005 11:36 am 
Newbie

Joined: Thu Jul 28, 2005 9:33 am
Posts: 4
Hello all,

I am using Hibernate 3.0.

I have two classes: Technology_Type, Technology associated with each other in a one-to-many relation.

Mapping for Technology_Type is below:

Code:
.
.
.
<set name="technologies" inverse="true" order-by="name desc"
   batch-size="2" where="name like upper('W%')" cascade="delete">
   <key column="type_id" on-delete="cascade" ></key>
   <one-to-many class="learn.hibernate.persistence.Technology"></one-to-many>
</set>
.
.
.


When I get the technologies beloning to a technology type everything is fine. But when I close the session (as shown in red), I get "...failed to lazily initialize a collection of role..."


// find the technologies under type say - 4
Technology_Type tt = (Technology_Type)ses.load(Technology_Type.class, new Long(4));

Set technologies = tt.getTechnologies();
Long tt_id = tt.getId();
String name = tt.getName();

ses.close();
System.out.println("There are "+technologies.size()+ " that belong to "+name +":");

When I have read all the properties of Technology_Type, I guess I should be able to close the session.

By placing the ses.close() after the System.out.println.... everything works fine.

Why?

J2EE_PRO
--------------------------
I think, therefore I survive




Code:
Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 11:57 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
If you really want to have your Set loaded right away you should be using lazy="false" in your <set> tag. Otherwise, you're telling Hibernate to wait until you need the data, then you close the session, and then you ask for the data. It can't go fetch the data if there's no session open.


Top
 Profile  
 
 Post subject: Re: ...failed to lazily initialize a collection of role...
PostPosted: Tue Aug 09, 2005 12:12 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
J2EE_pro wrote:
Hello all,

I am using Hibernate 3.0.

I have two classes: Technology_Type, Technology associated with each other in a one-to-many relation.

Mapping for Technology_Type is below:

Code:
.
.
.
<set name="technologies" inverse="true" order-by="name desc"
   batch-size="2" where="name like upper('W%')" cascade="delete">
   <key column="type_id" on-delete="cascade" ></key>
   <one-to-many class="learn.hibernate.persistence.Technology"></one-to-many>
</set>
.
.
.


When I get the technologies beloning to a technology type everything is fine. But when I close the session (as shown in red), I get "...failed to lazily initialize a collection of role..."


// find the technologies under type say - 4
Technology_Type tt = (Technology_Type)ses.load(Technology_Type.class, new Long(4));

Set technologies = tt.getTechnologies();
Long tt_id = tt.getId();
String name = tt.getName();

ses.close();
System.out.println("There are "+technologies.size()+ " that belong to "+name +":");

When I have read all the properties of Technology_Type, I guess I should be able to close the session.

By placing the ses.close() after the System.out.println.... everything works fine.

Why?

J2EE_PRO
--------------------------
I think, therefore I survive




Code:
Code:
Code:


Lazy loaded objects in reality are Hibernate proxies. Once you call session close you will have resources associated with it released. It means data behind proxies not accessible anymore. In your example collection of Technologies was not initialized yet. You got reference on proxy collection instead.

_________________
Vasyl Zhabko


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.