Announcement Announcement Module
Collapse
No announcement yet.
failed to initialise a lazy collection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • failed to initialise a lazy collection

    Hi,

    I'm new to spring and hibernate and currently am seeing the following exception when running a unit test against a service layer method

    Code:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: uk.co.x.server.dal.Site.substations, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    	at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
    	at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248)
    	at uk.co.x.server.dal.SiteTest.testLoad(SiteTest.java:78)
    All that my test is doing is loading a list of all of the "site" objects in the database.

    I believe i have to do something to stop the session closing after the list of sites have been retrieved (there is another collection contained within the site which im trying to get in my unit test). Ive seen examples of adding OpenSessionInViewFilter to web.xml, but as im running a junit test, i dont have a web.xml.

    Ive tried adding

    Code:
    @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    to my service layer method which gets all of the sites, but this doesnt help either.

    Could someone help me work out what im missing.

    Thanks.

    My application context is set up as follows

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans>  
        <bean name="siteDao" class="uk.co.x.server.dal.hibernate.SiteDaoImpl">
           <property name="sessionFactory">
               <ref bean="sessionFactory"/>
           </property>
        </bean>
     
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="dataSource">
               <ref bean="dataSource"/>
           </property>
           <property name="mappingResources">
               <list>
                   <value>/uk/co/x/server/dal/Site.hbm.xml</value>
                   <value>/uk/co/x/server/dal/AbstractModelBase.hbm.xml</value>
                   <value>/uk/co/x/server/dal/Agreement.hbm.xml</value>
                   <value>/uk/co/x/server/dal/Apparatus.hbm.xml</value>
                   <value>/uk/co/x/server/dal/Contact.hbm.xml</value>
                   <value>/uk/co/x/server/dal/Diagram.hbm.xml</value>
                   <value>/uk/co/x/server/dal/LocalRec.hbm.xml</value>
                   <value>/uk/co/x/server/dal/Substation.hbm.xml</value>
                   
               </list>
           </property>
           <property name="hibernateProperties">
               <props>
                   <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
                    <prop key="hibernate.default_schema">x</prop>
      				<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
      			
               </props>
          </property>
        </bean>
      
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
           <property name="driverClassName" value="org.postgresql.Driver"/>
           <property name="url" value="xxxxx" />
           <property name="username" value="xxxxx" />
           <property name="password" value="xxxxx" />
        </bean>
        
    
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
           <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
        
         
    </beans>

  • #2
    Hi mtrueman,

    It seems there is no session/transaction active when you are retrieving the lazy loaded collection.

    Even though you have your @Transactional annotation Spring is not scanning for them as you haven't enabled annotation transaction configuration.
    Try adding:
    Code:
    <tx:annotation-driven/>
    to your Spring config file.
    This will enable transaction configuration via annotation and Spring will scan and honor your annotated class/methods.

    I hope it helps.


    nicolas.loriente

    Comment

    Working...
    X