-->
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: Lazy Initialization Exception..Does not show up in unit test
PostPosted: Wed May 20, 2009 8:57 pm 
Newbie

Joined: Wed May 20, 2009 6:50 pm
Posts: 4
Hi all,
I am new to hibernate ad stuck up in LazyInitializationException

I have a relationship like this:

Organization {

/**one-to-many*/
Set dept; // of type Departments

}

Departments{

/**many-to-one */
Organization organization

/**many-to-one */
Activity activity;
}

Activity{

/** one-to-many */
set departments

/**many-to-one*/
ActivityDetails details;

}

ActivityDetails {
String venue;

}

All relations are non-lazy.


Given an organization, I want to fetch all activities and ActivityDetails associated with it.

My HQL is

"from Departments as dept where dept.organisation.name = ? "

I get all activities and related Activity details in my Junit tests.
However when I deploy same code in tomcat and try to fetch activity and details,
I get LazyInitializationException....at follwing statement
details = department.getActivity().getDetails();


However if i change my query to

from Departments as dept left join fetch dept.activity act left join fetch act.details details where spon.organisation.name = ?


the code passes thru unit test as well as on the server.


I don't understand how is it possible that old query code runs thru properly in unit test environment but fails on tomcat?


Top
 Profile  
 
 Post subject: Re: Lazy Initialization Exception..Does not show up in unit test
PostPosted: Thu May 21, 2009 12:47 am 
Newbie

Joined: Tue May 19, 2009 11:22 am
Posts: 6
Mebbe in your unit test, you have the session open till you access activity details. In production code, are you trying to retrieve the details outside the scope of session? In that case, we might encounter the LazyInitializationException.

Regd outer join, it will eagerly fetch the collection / association, so we will not encounter the lazy .. exception.


Top
 Profile  
 
 Post subject: Re: Lazy Initialization Exception..Does not show up in unit test
PostPosted: Thu May 21, 2009 10:34 pm 
Newbie

Joined: Wed May 20, 2009 6:50 pm
Posts: 4
Thanks for the reply.
You are correct.
I am usign Unitils test framework in which by default each test runs in its own transaction overriding any trasaction configurations present in classpath.
However, when i made the test case to be @Transactional(TransactionMode.DISABLED) I started getting same exception even in my test case.
Now about the second part: Why it failed on webapp
Well my target bean was configured to have "PROPAGATION_REQUIRED" only for update and delete and not for read which caused the exception on get*** methods.

<property name="transactionAttributes">
<props>
<prop key="store*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>


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.