-->
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.  [ 2 posts ] 
Author Message
 Post subject: Trouble understanding the lazy loading no session error
PostPosted: Fri May 20, 2011 6:05 pm 
Newbie

Joined: Thu May 19, 2011 12:59 am
Posts: 13
Quote:
failed to lazily initialize a collection of role: com.myapp.Customer.addresses, no session or session was closed


thats what I get when I try loading an object after saving one and I dont understand why.

this is in a JUnit test case - NOT a web page so the OSIV fix is not applicable here.

My code basically does the following:

Code:
MyDao dao = (MyDAO)springContext.getBean("myDAO");

MyObject obj = new MyObject();
// set attributes of obj
SubObject sobj = new SubObject(); // a child of obj
sobj.setSomeValue("something");
obj.addSubObject(sobj); // this adds sobj to obj's Set<SubObject> and set's sobj's parent
dao.saveObject(obj);

// EVERYTHING WORKS FINE UP TO THIS POINT
// I can verify those records were created in both tables

obj = dao.getObject(obj.getId()); // load the same object I wrote to table to verify it is saved correctly
// assert some values of obj
// SO FAR SO GOOD
// assert some values of sub-objects of obj
SubObject sobj = obj.getSubObject(0); // gets the first SubObject
assertNotNull(sobj.getSomeValue()); // ERROR! no session!



Why doesn't this work as expected? This is all in the same JUnit test case, I didnt do anything else between saving the objects and selecting new one.

How do I remedy this situation?


Top
 Profile  
 
 Post subject: Re: Trouble understanding the lazy loading no session error
PostPosted: Sun May 22, 2011 12:10 pm 
Newbie

Joined: Thu May 19, 2011 12:59 am
Posts: 13
Ok so I have learned a bit more on transactions and session management so I have a bit more understanding now. However not enough to resolve my problem apparently.

I read in a Hibernate book about transaction management with lazy associations and thought I found the solution. Unfortunately though it has proved to be ineffective.

It told me to make me entire test method transactional, by annotating with:
@Transactional(readOnly=false)

Then I added this to my application context xml:
Code:
  <tx:annotation-driven transaction-manager="transactionManager"/>
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>


Its the same single sessionFactory I use on my DAO.

This has not made a difference when I run the code however, I get the same LazyInitializationException

So what did I miss here? Are there other steps involved to activate transactions which the book failed to mention or perhaps I misunderstood?

[Edit] Also I am noticing that the transactional part is not doing its job, like its not activated. In my code I first do an insert, and then afterwards I do the select which I then start reading objects which results in the LazyInitializationException. So if my transaction was valid, it would have automatically rolled back my insert. However I am seeing that despite the exception being thrown, the insert has been commited.


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