-->
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
PostPosted: Mon May 10, 2004 6:19 pm 
Newbie

Joined: Thu May 06, 2004 1:20 pm
Posts: 7
I have checked the forums, and it appears that this question props up on a regular basis. However, I am unable to find an answer that applies to my case. Any help is greatly appreciated.

****** java code *****
public class Main
{
private SessionFactory factory;

public void populate() throws Exception
{
// open a session and save the team
Session s = null;
Transaction tx = null;
try
{
s = factory.openSession();
tx = s.beginTransaction();

User user = new User();
user.setLoginName("name");
user.setFirstName("First");
user.setLastName("Last");

Set emails = new Set();
Email email = new Email();
email.setEmail("first@host.com");
------exception--> emails.add(email);

//user.setEmails(emails);

s.saveOrUpdate(user);
tx.commit();
}
catch (Exception e)
{
if (tx!=null) tx.rollback();
throw e;
}
finally
{
s.close();
}
}

public static void main(String[] args) throws Exception {

final Main test = new Main();

Configuration cfg = new Configuration()
.addClass(User.class)
.addClass(Email.class);

//cfg.setProperty("hibernate.show_sql", "true");

test.factory = cfg.buildSessionFactory();

test.populate();

}
}

***** users mapping *****
<hibernate-mapping
package="users">

<class name="User" table="Users" proxy="User">
<id name="id">
<generator class="native"/>
</id>
<property name="loginName" not-null="true" unique="true"/>
<property name="firstName"/>
<property name="lastName"/>

<set name="emails" cascade="all">
<key column="userid"/>
<one-to-many class="Email"/>
</set>

</class>

</hibernate-mapping>

***** emails *****
<hibernate-mapping
package="users">

<class name="Email" table="Emails">
<id name="id">
<generator class="native"/>
</id>
<property name="email" not-null="true" unique="true"/>
</class>

</hibernate-mapping>

********* Log output (partial) *****

15:08:40,330 DEBUG SessionImpl:542 - opened session
15:08:40,340 DEBUG JDBCTransaction:37 - begin
15:08:40,340 DEBUG DriverManagerConnectionProvider:84 - total checked-out connections: 0
15:08:40,340 DEBUG DriverManagerConnectionProvider:90 - using pooled JDBC connection, pool size: 0
15:08:40,350 DEBUG JDBCTransaction:41 - current autocommit status:false
15:08:40,380 ERROR LazyInitializationException:25 - Failed to lazily initialize a collection - no session or session was closed
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.write(PersistentCollection.java:84)
at net.sf.hibernate.collection.Set.add(Set.java:154)
at Main.populate(Main.java:49)
at Main.main(Main.java:80)
15:08:40,390 DEBUG JDBCTransaction:82 - rollback
15:08:40,390 DEBUG SessionImpl:572 - transaction completion
15:08:40,390 DEBUG SessionImpl:560 - closing session
15:08:40,390 DEBUG SessionImpl:3327 - disconnecting session
15:08:40,400 DEBUG DriverManagerConnectionProvider:120 - returning connection to pool, pool size: 1
15:08:40,400 DEBUG SessionImpl:572 - transaction completion
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.write(PersistentCollection.java:84)
at net.sf.hibernate.collection.Set.add(Set.java:154)
at Main.populate(Main.java:49)
at Main.main(Main.java:80)
Exception in thread "main"


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 12:10 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
i think you are accidentally using the hibernate collection set instead of the java.util.Set. check your imports.

Code:
User user = new User();
user.setLoginName("name");
user.setFirstName("First");
user.setLastName("Last");

Set emails = new Set();
Email email = new Email();
email.setEmail("first@host.com");
------exception--> emails.add(email);

//user.setEmails(emails);


a hibernate set collection during initlaization checks to see if it is associated with the session (this is what makes it "live").


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 2:25 pm 
Newbie

Joined: Thu May 06, 2004 1:20 pm
Posts: 7
Thank you very much. That was exactly my problem. Saved me a day of another grief!


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.