-->
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: EAGER fetching -- not working?!?
PostPosted: Mon Oct 05, 2009 10:51 am 
Newbie

Joined: Tue Nov 22, 2005 12:47 am
Posts: 6
i have a many-to-one class relationship that goes (other details snipped out):
note the FetchType being EAGER...

Code:
public class Customer{
   @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   @JoinColumn(name="customer_id")
   @org.hibernate.annotations.IndexColumn(name="payPosition")
   @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
   @OrderBy("paymentDate")
   public List<CustomerPaymentHistory> getPaymentHistory() {
      return paymentHistory;
   }
}


however, when i try to iterate on the list,

Code:
      //retrieve data
      Customer cust = customerDao.findById(c1.getCustomerId());
      Assert.assertNotNull(cust);
      
      for(CustomerPaymentHistory cph : cust.getPaymentHistory()){
         System.out.println("\t" + cph );
      }


I get a no Session error which points to the for loop above considering that its not Lazily retrieved:
Quote:
performCustomerOperations(com.ifvi.rims.dao.test.CustomerDaoTest) Time elapsed: 0.976 sec <<< ERROR!
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:108)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:150)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at com.ifvi.rims.entities.Customer$$EnhancerByCGLIB$$bad860c2.getPaymentHistory(<generated>)
at com.ifvi.rims.dao.test.CustomerDaoTest.performCustomerOperations(CustomerDaoTest.java:130)


What did I miss in here?
Hope the info above is enough to state my point. Thanks!


Top
 Profile  
 
 Post subject: Re: EAGER fetching -- not working?!?
PostPosted: Tue Oct 06, 2009 12:14 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
two things to be noted:-
1)In order to fix the issue you have one need to know what the customerDao.findById is actually doing? is it using a hibernate Load method or a get method? You might want to double check and use a get method on the object so that you get an initialized collection.
Code:
      //retrieve data
      Customer cust = customerDao.findById(c1.getCustomerId());
      Assert.assertNotNull(cust);
     
      for(CustomerPaymentHistory cph : cust.getPaymentHistory()){
         System.out.println("\t" + cph );
      }


also in your mapping fetch=FetchType.EAGER does not tell hibernate that you meant lazy="false" unless you have the lazy attribute set at the entity level.
So change the annotation or the mapping file for the attribute paymentHistory to have lazy="false" AND fetch=FetchType.EAGER. Then try both get and load methods appropriately.

2)For performance reasons its always better to use Lazy="true" and on the DAO method use the Fetch strategy to override the default behaviour. Something like:-
Criteria criteria = session.createCriteria(Customer.class);
customer = (Customer) criteria.setFetchMode("paymentHistory", FetchMode.<<appropriate mode>>).add(Restrictions.idEq(customerId))
.uniqueResult();

Hope this helps,
Srilatha.


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.