1 Reply Latest reply on Dec 27, 2005 5:03 PM by Richard Boehme

    Problems Lazy loading in Hibernate Server Side

    Richard Boehme Newbie

      Hi there. I'm using Hibernate with Spring and JBoss, and am having problems with lazy loading. I'm using XDoclet to generate my Hibernate configuration files, and have searched on this topic extensively. The best resource so far that I have found is here:

      http://www.jroller.com/page/kbaum/20040708

      an interesting entry is:

      http://www-128.ibm.com/developerworks/java/library/j-hibern/

      This describes how to implement lazy loading in the business layer. When I implement, however, I still get a LazyLoadingException (config files below):

      @APPNAME@] 23484 ERROR [AWT-EventQueue-0] LazyInitializationException.(25) | Failed to lazily initialize a collection - no session or session was closed
      net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
      at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
      at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
      at net.sf.hibernate.collection.Bag.iterator(Bag.java:256)


      I have also tried using the OpenSessionInViewInterceptor and OpenSessionInViewFilter. However, because data objects are accessed using a manager class which accesses the DAO class, I didn't have high expectations of that method's working.



      In our Hibernate application context, we have:

      <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->





      along with DAO implementation beans, like so:

      <!-- UserDAO: Hibernate implementation -->





      We have a "service" application context, wherein is:















      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED
      PROPAGATION_REQUIRED,readOnly





      //I've tried putting a reference to the hibernateInterceptor here. That doesn't help.







      com.adaptiverfid.service.ShipmentManager



      hibernateInterceptor






      Any help is appreciated.

      Thanks.

      Richard

        • 1. Re: Problems Lazy loading in Hibernate Server Side
          Richard Boehme Newbie

          Sorry about the mispost; this should allow you to read the examples I provided. Unfortunately, I'm having trouble posting my example without the text vanishing, os please bear with me:



          Hi there. I'm using Hibernate with Spring and JBoss, and am having problems with lazy loading. I'm using XDoclet to generate my Hibernate configuration files, and have searched on this topic extensively. The best resource so far that I have found is here:

          http://www.jroller.com/page/kbaum/20040708

          an interesting entry is:

          http://www-128.ibm.com/developerworks/java/library/j-hibern/

          This describes how to implement lazy loading in the business layer. When I implement, however, I still get a LazyLoadingException (config files below):

          @APPNAME@] 23484 ERROR [AWT-EventQueue-0] LazyInitializationException.&lt;init&gt;(25) | Failed to lazily initialize a collection - no session or session was closed
          net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
          at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
          at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
          at net.sf.hibernate.collection.Bag.iterator(Bag.java:256)


          I have also tried using the OpenSessionInViewInterceptor and OpenSessionInViewFilter. However, because data objects are accessed using a manager class which accesses the DAO class, I didn't have high expectations of that method's working.



          In our Hibernate application context, we have:

          &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt;
          &lt;bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory"&gt;&lt;ref local="sessionFactory"/&gt;&lt;/property&gt;
          &lt;/bean&gt;


          along with DAO implementation beans, like so:

          &lt;!-- UserDAO: Hibernate implementation --&gt;
          &lt;bean id="userDAO" class="com.adaptiverfid.dao.hibernate.UserDAOHibernate"&gt;
          &lt;property name="sessionFactory"&gt;&lt;ref local="sessionFactory"/&gt;&lt;/property&gt;
          &lt;/bean&gt;


          We have a "service" application context, wherein is:

          &lt;bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor"&gt;
          &lt;property name="sessionFactory"&gt;
          &lt;ref bean="sessionFactory"/&gt;
          &lt;/property&gt;
          &lt;/bean&gt;

          &lt;bean id="shipmentManagerObj" parent="txProxyTemplate"&gt;
          &lt;property name="target"&gt;
          &lt;bean class="com.adaptiverfid.service.impl.ShipmentManagerImpl"&gt;
          &lt;property name="shipmentDAO"&gt;&lt;ref bean="shipmentDAO"/&gt;&lt;/property&gt;
          &lt;/bean&gt;
          &lt;/property&gt;
          &lt;property name="transactionAttributes"&gt;
          &lt;props&gt;
          &lt;prop key="save*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="update*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="remove*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="create*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="print*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="finish*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="validate*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="get*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="*"&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;
          &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="preInterceptors"&gt;
          &lt;list&gt;
          &lt;ref bean="shipmentStatusChangeWatcher"/&gt;
          //I've tried putting a reference to the hibernateInterceptor here. That doesn't help.
          &lt;/list&gt;
          &lt;/property&gt;
          &lt;/bean&gt;

          &lt;bean id="shipmentManager" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;
          &lt;property name="proxyTarget"&gt;&lt;ref bean="shipmentManagerObj"/&gt;&lt;/property&gt;
          &lt;property name="proxyInterfaces"&gt;
          &lt;value&gt;com.adaptiverfid.service.ShipmentManager&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="interceptorNames"&gt;
          &lt;list&gt;
          &lt;value&gt;hibernateInterceptor&lt;/value&gt;
          &lt;/list&gt;
          &lt;/property&gt;
          &lt;/bean&gt;



          Any help is appreciated.

          Thanks.

          Richard