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>${hibernate.connection.driver_class}</value>
</property>
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</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">
${hibernate.dialect}
</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<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 = (Service) context.getBean("Service");
User[] users = service.getUsers();
System.out.println("name is " + users[0].getEmailAddress());
System.out.println("role name is " + users[0].getRoles()[0].getName()); // 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() throws DataAccessException {
List users = (List) getHibernateTemplate().find("from User");
return users;
}
Please help...
Thanks,
SSSS.