-->
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: org.hibernate.LazyInitializationException: failed to laz....
PostPosted: Tue Feb 19, 2008 6:16 pm 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
Hi,

i have problem with Lazy loading of collections (List).

I have POJO object - class A, this object has property - Lists of objects B.

Code:
@OneToMany(mappedBy = "objectB", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinColumn(name = "fk_b_id")
   private List<B> objectsB;


i use LAZY loading for this object.

so, i try load object A by id:

A objectA = objectAManager.getA(id);

in get function I use: getHibernateTemplate().get

how can I load all collections for object A.

after JSP code:

Code:
<c:forEach items="${A.objectsB}" var="objectA" varStatus="current">
</c:forEach> 


this error message is displayed:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.freightservice.model.A.ObjectsB, no session or session was closed

i use interceptor:

Code:
<beans>
   <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
       <property name="sessionFactory">
           <ref bean="sessionFactory"/>
       </property>
       <property name="singleSession">
           <value>false</value>
       </property>
   </bean>
</beans>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="interceptors">
           <list>
               <ref bean="openSessionInViewInterceptor"/>
           </list>
       </property>
      <property name="mappings">
....
      </property>
</bean>


thanks!

Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 6:44 pm 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
is this solution OK ?

for (int i = 0; i < objectA.getObjectsB().size(); i++) {
ObjectB objectB = objectA.getObjectsB().get(i);
org.hibernate.Hibernate.initialize(objectB);

}

Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 7:00 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
termi wrote:
is this solution OK ?


Yes and no. It may solve your problem but the open session in view pattern is meant to solve this and the reason people go for it is that it exploits the benefits of lazy loading - it loads only the entities that are needed. So, you might want to find why this is not working. I have no good idea where you problem is but you might want to include the full stack trace. That will tell us where the call is coming from and that might show where the problem is.


Farzad-


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.