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

  • failed to initialise a lazy collection


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

    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(
    	at org.hibernate.collection.AbstractPersistentCollection.readSize(
    	at org.hibernate.collection.PersistentBag.size(
    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

    @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.


    My application context is set up as follows

    <?xml version="1.0" encoding="UTF-8"?>
        <bean name="siteDao" class="">
           <property name="sessionFactory">
               <ref bean="sessionFactory"/>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="dataSource">
               <ref bean="dataSource"/>
           <property name="mappingResources">
           <property name="hibernateProperties">
                   <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>
        <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 id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
           <property name="sessionFactory"><ref local="sessionFactory"/></property>

  • #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:
    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.