In my DAO layer, I have the following method which does takes out all of the domain objects.
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.Code:public List<Contractor> getAllWithLists() throws DataAccessException { HibernateTemplate ht = new HibernateTemplate(getHibernateTemplate().getSessionFactory()); return (List) ht.execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query query = session.createQuery( "from Contractor contractor order by contractor.id"); List<Contractor> contractors = query.list(); for (Contractor contractor : contractors) { contractor.getBankInfos(); contractor.getLicenses(); contractor.getCreditCards(); contractor.getInsurances(); } return contractors; } }); }
My problem is when I call the code:
When it gets to the line where it's printing out the getBankInfo().size() it throws an exception.Code:ContractorDao contractorDao = (ContractorDao) context.getBean("contractorDao"); List<Contractor> contractors = contractorDao.getAllWithLists(); for (Contractor contractor : contractors) { System.out.println(contractor.getFirstName()); System.out.println(contractor.getBankInfos().size()); }
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?Code:Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.localtree.domain.pojo.contractor.Contractor.bankInfos - no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:191) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:183) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48) at org.hibernate.collection.PersistentSet.size(PersistentSet.java:110) at com.localtree.test.dao.TestContractorDao.main(TestContractorDao.java:82)


Reply With Quote