Results 1 to 2 of 2

Thread: hibernate and lazyloading problem

  1. #1
    Join Date
    Jul 2005
    Posts
    20

    Default hibernate and lazyloading problem

    In my DAO layer, I have the following method which does takes out all of the domain objects.

    Code:
    public List<Contractor> getAllWithLists&#40;&#41; throws DataAccessException
       &#123;
          HibernateTemplate ht = new HibernateTemplate&#40;getHibernateTemplate&#40;&#41;.getSessionFactory&#40;&#41;&#41;;
          return &#40;List&#41; ht.execute&#40;new HibernateCallback&#40;&#41; &#123;
              public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123;
                  Query query = session.createQuery&#40;
                      "from Contractor contractor order by contractor.id"&#41;;
                  
                  List<Contractor> contractors = query.list&#40;&#41;;
                  for &#40;Contractor contractor &#58; contractors&#41;
                  &#123;
                     contractor.getBankInfos&#40;&#41;;
                     contractor.getLicenses&#40;&#41;;
                     contractor.getCreditCards&#40;&#41;;
                     contractor.getInsurances&#40;&#41;;
                  &#125;
                  return contractors;
              &#125;
          &#125;&#41;;
       &#125;
    The reason why I have the getBankInfos(), getLicenses(), etc is because I currently have the Contractor class to be lazy-loaded and I'm "touching" them so that they will be loaded.

    My problem is when I call the code:

    Code:
    ContractorDao contractorDao = &#40;ContractorDao&#41; context.getBean&#40;"contractorDao"&#41;;
    
    List<Contractor> contractors = contractorDao.getAllWithLists&#40;&#41;;
          
    for &#40;Contractor contractor &#58; contractors&#41;
    &#123;
        System.out.println&#40;contractor.getFirstName&#40;&#41;&#41;;
        System.out.println&#40;contractor.getBankInfos&#40;&#41;.size&#40;&#41;&#41;;
    &#125;
    When it gets to the line where it's printing out the getBankInfo().size() it throws an exception.

    Code:
    Exception in thread "main" org.hibernate.LazyInitializationException&#58; failed to lazily initialize a collection of role&#58; com.localtree.domain.pojo.contractor.Contractor.bankInfos - no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException&#40;AbstractPersistentCollection.java&#58;191&#41;
    	at org.hibernate.collection.AbstractPersistentCollection.initialize&#40;AbstractPersistentCollection.java&#58;183&#41;
    	at org.hibernate.collection.AbstractPersistentCollection.read&#40;AbstractPersistentCollection.java&#58;48&#41;
    	at org.hibernate.collection.PersistentSet.size&#40;PersistentSet.java&#58;110&#41;
    	at com.localtree.test.dao.TestContractorDao.main&#40;TestContractorDao.java&#58;82&#41;
    I know that the bankInfo List is populated in the DAO layer but when I pass it up, Hibernate tries to lazy-load it. I'm confused because I want the object to be detached from the Hibernate Session by the time I pass it up from to the calling code. How do I achieve that?

  2. #2
    Join Date
    Jul 2005
    Posts
    20

    Default

    I think I realized my problem. My code (calling code and DAO code) need to participate in a transaction for Spring to associate the two in one Hibernate Session. Does that sound right?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •