-->
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: Getting LazyInitialization Exception
PostPosted: Thu Dec 18, 2008 6:17 am 
Newbie

Joined: Tue Sep 16, 2008 5:46 am
Posts: 3
Hibernate version: 3.2.5

Name and version of the database: SQL Server

Hi,

I have posted the same question at Spring forum as well. I am not sure if it is a Hibernate Problem.

I have a Standalone application where I am using Spring with Hibernate. I have an entity that contains another entity as a set.


Code:
<class name="Demo" table="DEMO">
...
...
<set name="addresses" inverse="true" cascade="save-update, delete, delete-orphan">
      <key>
         <column name="filing_id"/>
      </key>
      <one-to-many class="Address"/>   
   </set


I retrieve an object of type 'demo' at one place. And after some processing I retrieve addresses from the 'demo' object. At that point I get a LazyInitialization exception.


Code:
// Retrieving demoDAO and demoobject
DemoDAO dao = DAOFactory.instance().getDemoDAO();
Demo demo = dao.findById("100");
...
...
...
if (demo.getAddresses() != null && !demo.getAddresses().isEmpty()) {
   demo.getAddresses().clear();
}


I get the following exception -
Quote:
(hibernate.LazyInitializationException 19 ) failed to lazily initialize a collection of role: com.novus.sec.filings.model.Filing.addresses, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.novus.sec.filings.model.Filing.addresses, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationException(AbstractPersi stentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationExceptionIfNotConnected (AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollect ion.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentSet.isEmpty(Per sistentSet.java:146)


I am using Spring Transaction. Please find below my ApplicationContext.file -
Quote:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="demoTarget" class="com.xxx.sec.filings.dao.hibernate.DemoDAOHi bernate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


<bean id="demoDAO" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="proxyInterfaces">
<value>com.xxx.sec.filings.dao.demoDAO</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
<value>demoDAOTarget</value>
</list>
</property>
</bean>


I am kind of new to using Spring Hibernate Template. Can anyone please help me?

Thanks,
Manu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 7:40 am 
Newbie

Joined: Tue Nov 11, 2008 5:36 am
Posts: 5
Location: Stockholm, Sweden
Make sure you use your address entity within a transaction, otherwise it won't work. Also make sure you not trying to use an entity that is not loaded into the session. You have to load the address entity before you can use e.g. demo.getAddresses().isEmpty().

To load the address entity into the session, try something like:
getDao().getHibernateTemplate().load(Address.class, demo.getAddress().getId());


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 7:48 am 
Newbie

Joined: Mon Oct 01, 2007 8:59 am
Posts: 18
Make sure session should be open before this point


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.