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: Hibernate lazy instantiation problem
PostPosted: Thu Oct 06, 2005 10:11 am 
Newbie

Joined: Thu Oct 06, 2005 7:11 am
Posts: 2
I'm moving a resulset from an Iterator into a Collection of Beans.

Code:
public Collection daoIteratorToBeanCollectionHoch(Iterator iterate) {
      Collection dst = new Vector();
      
      for (; iterate.hasNext(); ) {
         Rechnung rech = (Rechnung) iterate.next();

         ReportingBean_hoch rep = fillBean(rech);   

         dst.add(rep);
      }
      
      return dst;
   }

   private ReportingBean_hoch fillBean(Rechnung rech) {
      ReportingBean_hoch rep = new ReportingBean_hoch();
      
      if (rech.getLieferant() != null) {
         rep.setLief_bezeichnung(rech.getLieferant().getBezeichnung());
         rep.setLief_fibuKonto(rech.getLieferant().getFibuKonto());
      }

      if (rech.getMandant() != null) {
         rep.setMand_bezeichnung(rech.getMandant().getBezeichnung());
         rep.setMand_mdNr(rech.getMandant().getMdNr());
      }

      rep.setRech_barcode(rech.getBarcode());
      rep.setRech_bruttoBetrag(rech.getBruttoBetrag());
      rep.setRech_buchtext(rech.getBuchtext());
      rep.setRech_eigeneBelegNummer(rech.getEigeneBelegNummer());
      rep.setRech_rechnungsDatum(rech.getRechnungsDatum());
      rep.setRech_rechnungsNummer(rech.getRechnungsNummer());
      rep.setRech_skonto_netto(rech.getZkTageNetto());
      
      return rep;
   }
   
      
   public Collection getReportRechAlle(HashMap params) {
      sql = sqlSelectFieldsHoch + "from Rechnung r";
      Iterator iterate = getHibernateTemplate().find("from Rechnung r").iterator();
      
      Collection col =  daoIteratorToBeanCollectionHoch(iterate);

      return col;
   }
   


So far so good, but when I use the "normal" way
Code:
getHibernateTemplate().iterate("from Rechnung r");

in the Method getReportRechAlle I get a "LazyInitializationException"

Trace:
Code:
net.sf.hibernate.LazyInitializationException: Hibernate lazy instantiation problem
   at net.sf.hibernate.impl.IteratorImpl.next(IteratorImpl.java:133)
   at com.achtg.fibunet.webreb.hibernate.dao.impl.ReportingHochDAOImpl.daoIteratorToBeanCollectionHoch(ReportingHochDAOImpl.java:44)
   at com.achtg.fibunet.webreb.hibernate.dao.impl.ReportingHochDAOImpl.getReportRechAlle(ReportingHochDAOImpl.java:91)
   at com.achtg.fibunet.webreb.test.dao.ReportingHochDAOTest.testGetReportRechAlle(ReportingHochDAOTest.java:43)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: Invalid state, the ResultSet object is closed.
   at net.sourceforge.jtds.jdbc.JtdsResultSet.checkOpen(JtdsResultSet.java:295)
   at net.sourceforge.jtds.jdbc.JtdsResultSet.next(JtdsResultSet.java:555)
   at net.sf.hibernate.impl.IteratorImpl.postNext(IteratorImpl.java:85)
   at net.sf.hibernate.impl.IteratorImpl.next(IteratorImpl.java:127)
   ... 18 more


I can't see what may be wrong, anyone an idea?
Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 10:52 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
"getHibernateTemplate()" if this Spring stuff then propbably you need to implement callback, see Spring docs.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 11:41 am 
Beginner
Beginner

Joined: Tue Aug 17, 2004 11:27 pm
Posts: 24
Your issue is that the session has been closed after your call to HibernateTemplate. Notice your exception is not being thrown from the call to hibernate template. It's being thrown from your call to iterate. You need to open up a session and bind it to the thread before your call to iterate. Otherwise hibernate template will open and close a session for each database execution.

Take a look at this blog I wrote a while back.

http://www.jroller.com/page/kbaum?entry ... n_with_dao

In the section entitled being lazy in your unit tests, the setup method opens a hibernate session and binds it to the thread.


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.