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

  • lazy loading with hibernate

    Hi,
    I have a problem in lazy loading...follwing is my configuration...

    Code:
    <beans>	
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName">
    			<value>$&#123;hibernate.connection.driver_class&#125;</value>
    		</property>
    		<property name="url">
    			<value>$&#123;hibernate.connection.url&#125;</value>
    		</property>
    		<property name="username">
    			<value>$&#123;hibernate.connection.username&#125;</value>
    		</property>
    		<property name="password">
    			<value>$&#123;hibernate.connection.password&#125;</value>
    		</property>
    
    	</bean>
    
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref local="dataSource" />
    		</property>
    
    		<property name="mappingResources">
    			<list>
    				<value>
    					com/testing/impl/Service.hbm.xml
    				</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					$&#123;hibernate.dialect&#125;
    				</prop>
    			</props>
    		</property>
    
    	</bean>
    
    	<!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    	<!-- Add DAOs here -->
    	<bean id="userDao"
    		class="com.testing.dao.hibernate.HibernateRoleDAO">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="Service"
    		class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="target">
    			<ref bean="userManagerTarget" />
    		</property>
    		 <property name="proxyInterfaces">
               <value>com.testing.Service</value>
             </property>
    		
    		<property name="interceptorNames">
    		<list>
    			<value>hibernateInterceptor</value>
    		</list>
    	</property>
    	</bean>
    
    	  <bean id="hibernateInterceptor"
    		class="org.springframework.orm.hibernate.HibernateInterceptor">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    
    <bean id="userManagerTarget"
    		class="com.testing.ServiceImpl">
    	<property name="userDao">
    		<ref bean="userDao" />
    	</property>
    </bean>
    I am getting "Failed to lazily initialize a collection - no session or session was closed" exception with the following code

    Code:
    Service service = &#40;Service&#41; context.getBean&#40;"Service"&#41;;
        	User&#91;&#93; users = service.getUsers&#40;&#41;;
        	System.out.println&#40;"name is " + users&#91;0&#93;.getEmailAddress&#40;&#41;&#41;;
        	System.out.println&#40;"role name is " + users&#91;0&#93;.getRoles&#40;&#41;&#91;0&#93;.getName&#40;&#41;&#41;; // Getting the exception here
    But this is what exactly was suggested in
    http://www.jroller.com/page/kbaum/20040708

    Then where am I doing wrong? Why am I not able to lazily load the objects?
    Following is my DAO code
    Code:
    public List getUsers&#40;&#41; throws DataAccessException &#123;
            List users = &#40;List&#41; getHibernateTemplate&#40;&#41;.find&#40;"from User"&#41;;
            return users;
        &#125;
    Please help...
    Thanks,
    SSSS.

  • #2
    Hi,

    for lazy loading to work in a webapp you will have to put a OpenSessionInViewFilter/OpenSessionInViewInterceptor into your web.xml.
    If you're not in a webapp you will have to init it manually:

    init:
    Code:
    SessionFactory sessionFactory = &#40;SessionFactory&#41; getBean&#40;"sessionFactory"&#41;;
    
    Session session = sessionFactory.openSession&#40;&#41;;
    TransactionSynchronizationManager.bindResource&#40;sessionFactory, new SessionHolder&#40;session&#41;&#41;;
    clean up:
    Code:
    SessionHolder holder = &#40;SessionHolder&#41; TransactionSynchronizationManager.getResource&#40;sessionFactory&#41;;
    Session session = holder.getSession&#40;&#41;;
    
    session.flush&#40;&#41;;
    
    TransactionSynchronizationManager.unbindResource&#40;sessionFactory&#41;;
    SessionFactoryUtils.closeSessionIfNecessary&#40;session, sessionFactory&#41;;
    regards,
    Mario

    Comment


    • #3
      Hi,

      I'm using the OpenSessionInViewFilter, and still can't do lazy initialization from my views.

      I tried the "manual" version you sugest from a test class, and it works.

      I tried adding breakpoints to OpenSessionInViewFilter, and can see it gets the sessionFactory, opens the session, and binds it to the sessionFactory, but still it doesn't work.

      I took a look at TransactionSynchronizationManager, and got the impression that for the OpenSessionInViewFilter to work I had to use transactions. Could this be the issue ?

      thanks
      Ricardo

      Comment

      Working...
      X