Announcement Announcement Module
Collapse
No announcement yet.
lazy load problems Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • lazy load problems

    What exactly does the following exception mean... or rather, how do I fix it?

    I am using "HibernateDaoSupport" for accessing the database, and have configured the "OpenSessionInViewInterceptor" which I thought would allow me to use lazy collections in my web-app, but I still get this error:

    net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
    net.sf.hibernate.collection.PersistentCollection.i nitialize(PersistentCollection.java:209)
    net.sf.hibernate.collection.PersistentCollection.r ead(PersistentCollection.java:71)
    net.sf.hibernate.collection.Set.size(Set.java:106)
    dk.ciber.danbro.maillist.web.MaillistEditControlle r.handleSubmitFacilities(MaillistEditController.ja va:145)
    dk.ciber.danbro.maillist.web.MaillistEditControlle r.onSubmit(MaillistEditController.java:65)
    org.springframework.web.servlet.mvc.SimpleFormCont roller.processFormSubmission(SimpleFormController. java:222)
    org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:237)
    org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:121)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:45)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:485)
    org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:322)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:856)


    Thanks,
    Peter

  • #2
    Could you show the relevant parts of your configuration files?

    Comment


    • #3
      Hi again,

      I have made a simple example which illustrates my problem. I'm not sure how much I should include here, so snippets follow. If anyone thinks they can help and wants more info (or the entire application), don't hesitate to ask.

      I have two classes: AObject and BObject
      AObject contains a collection of BObjects, and BObject contains a collection of AObjects.

      I get an exception, for example, when I try to add a BObject to the collection in my AObject, and save the AObject to the database.
      2004-12-02 14:59:21,890 ERROR [org.springframework.web.servlet.DispatcherServlet] - <Could not complete request>
      net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
      at net.sf.hibernate.collection.PersistentCollection.i nitialize(PersistentCollection.java:209)
      at net.sf.hibernate.collection.PersistentCollection.w rite(PersistentCollection.java:84)
      at net.sf.hibernate.collection.Set.add(Set.java:154)
      at dk.ciber.hibertest.AObject.addBobject(AObject.java :19)
      at dk.ciber.hibertest.AEditController.onSubmit(AEditC ontroller.java:31)
      at org.springframework.web.servlet.mvc.SimpleFormCont roller.processFormSubmission(SimpleFormController. java:222)
      at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:237)
      ...

      Here is part of my *-servlet xml file:

      <beans>
      <bean id="AEditController" class="dk.ciber.hibertest.AEditController">
      <property name="sessionForm"><value>true</value></property>
      <property name="commandName"><value>base</value></property>

      <property name="commandClass">
      <value>dk.ciber.hibertest.AObject</value>
      </property>

      <property name="formView">
      <value>AEdit</value>
      </property>

      <property name="successView">
      <value>AView.htm</value>
      </property>

      <property name="manager"><ref bean="manager"/></property>
      </bean>

      ...

      <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
      <property name="interceptors">
      <list>
      <ref bean="openSessionInViewInterceptor"/>
      </list>
      </property>

      <property name="mappings">
      <props>
      <prop key="/AEdit.htm">AEditController</prop>
      ...
      </props>
      </property>
      </bean>

      <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate.support.O penSessionInViewInterceptor">
      <property name="sessionFactory"><ref bean="hibernateSessionFactory"/></property>
      <property name="flushMode"><value>1</value></property>
      <property name="singleSession"><value>false</value></property>
      </bean>

      <bean id="viewResolver" class="org.springframework.web.servlet.view.Resour ceBundleViewResolver">
      <property name="basename"><value>views</value></property>
      </bean>

      <bean id="manager" class="dk.ciber.hibertest.Manager">
      <constructor-arg><ref bean="dao" /></constructor-arg>
      </bean>

      <bean id="dao" class="dk.ciber.hibertest.HibernateDao">
      <property name="sessionFactory">
      <ref local="hibernateSessionFactory"/>
      </property>
      </bean>

      ...

      <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
      <property name="mappingResources">
      <list>
      <value>dk/ciber/hibertest/data.hbm.xml</value>
      </list>
      </property>

      <property name="hibernateProperties">
      <props>
      <prop key="hibernate.dialect">net.sf.hibernate.dialect.H SQLDialect</prop>
      <prop key="hibernate.query.substitutions">true=1 false=0</prop>
      <prop key="hibernate.show_sql">false</prop>
      <prop key="hibernate.cache.provider_class">net.sf.ehcach e.hibernate.Provider</prop>
      </props>
      </property>

      <property name="dataSource"><ref bean="dataSource"/></property>
      </bean>

      <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
      <property name="sessionFactory"><ref local="hibernateSessionFactory"/></property>
      </bean>
      </beans>


      Here is my Hibernate mapping file, which maps my objects to my database:

      <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
      <hibernate-mapping>
      <class name="dk.ciber.hibertest.AbstractBase" table="BASE" >
      <id name="id" type="long" column="ID">
      <generator class="increment"/>
      </id>

      <joined-subclass name="dk.ciber.hibertest.AObject" table="A_OBJECT">
      <key column="ID" />
      <property name="name" column="NAME" type="string" />
      <set name="bObjects" table="A_B" lazy="true" >
      <key column="A_ID"/>
      <many-to-many column="B_ID" class="dk.ciber.hibertest.BObject"/>
      </set>
      </joined-subclass>

      <joined-subclass name="dk.ciber.hibertest.BObject" table="B_OBJECT">
      <key column="ID" />
      <property name="name" column="NAME" type="string" />
      <set name="aObjects" table="A_B" lazy="true">
      <key column="B_ID"/>
      <many-to-many column="A_ID" class="dk.ciber.hibertest.AObject"/>
      </set>
      </joined-subclass>
      </class>
      </hibernate-mapping>

      Thanks for any help or advice,
      Peter

      Comment


      • #4
        Peter,

        I see that you configured a HibernateTransactionManager, but I do not see where you apply transaction to your dk.ciber.hibertest.Manager.

        Comment

        Working...
        X