Announcement Announcement Module
Collapse
No announcement yet.
Help need with @Transactional usage (LazyInitializationException) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help need with @Transactional usage (LazyInitializationException)

    I am new to hibernate with spring.
    When trying to implement Lazy Initialization according to Spring In Action book,
    it says to use
    Code:
    <tx:advice id="txAdvice">
    </tx:advice>
    
    <tx:annotation-driven transaction-manager="txManager" />
    
    
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- We are  using some extension to  org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean  for dynamic locating Entity beans -->
    <bean id="sessionFactory" class="com.iqlabs.iqrate.common.ExtendedAnnotationSessionFactoryBean">
    	<property name="dataSource" ref="dataSource" />
    	<property name="annotatedClasses">
    	<list>
    	</list>
    	</property>
    	<property name="basePackages">
    		<value>com.iqlabs.iqrate.anthill.persistance</value>
    	</property>
    	<property name="hibernateProperties">
    		<props>
    			<!-- <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>  Old postgres version-->
    			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    			<prop key="hibernate.hbm2ddl.auto">update</prop>
    			<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
    			<prop key="hibernate.cache.use_second_level_cache">true</prop>
    			<prop key="hibernate.generate_statistics">true</prop>
    			<prop key="hibernate.cache.use_structured_entries">true</prop>
    			<prop key="hibernate.cache.use_query_cache">true</prop>
    			
    		</props>
    	</property>
    </bean>
    Code:
    // Component("dataMappingsDao") // I remakred this line as suggested on some forums, but it's not helped
     "at"Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
    public class DataMappingsDao extends HibernateDaoSupport {
    
         "at"Transactional(propagation= Propagation.REQUIRED, readOnly=false)
        public void someTransactionTestMethod()
        {
              HotelMapping hm = (HotelMapping) getHibernateTemplate().load(HotelMapping.class,new Integer(2951));
    
    
            log.debug("Name:"+hm.getName());
    
            log.debug("Address:"+hm.getHotelInfo().getFull_address());
            log.debug("Photos size:"+hm.getHotelInfo().getPhotos().size());
    
        }
    }
    And I always get the
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.iqlabs.iqrate.anthill.persistance.datamapping. HotelInfo.photos, no session or session was closed

    After looking for forums,etc
    I found the solution
    I just have to add this code to my initialization. Looks mysterious for me ;-)
    session = SessionFactoryUtils.getSession(sessionFactory, true);
    TransactionSynchronizationManager.bindResource(ses sionFactory, new SessionHolder(session));
    As I understand, it just make one session for my application, and no session pool?


    What I am missing in my configuration?
    Is there any way to do it by annotation or at least in spring configuration, rather adding this code to initialization.
Working...
X