Hello!
I've been trying to work with OpenSessionInViewFilter and having some limited success, but my problem comes when trying to lazy load objects that are returned using the getHibernateTemplate().find() call. I'll be using my "Slot" class as an example, but this is happening with anywhere that I'm trying to use "find()" to return a list of my objects.
Also, if I use "load()" to get an object, I can lazy load any of it's related objects easily enough.
For this example, I'm trying to access the 'events' property of the Slot, and recieving org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.winterwar.base.Slot.events, no session or session was closed
exception.
If this is not enough information, let me know and I can post more relevant info.
web.xml
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost/appname</value></property>
<property name="username"><value>notreal</value></property>
<property name="password"><value>alsonotreal</value></property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<!-- Add list of .hbm.xml files here -->
<value>mappings/Convention.hbm.xml</value>
<value>mappings/Slot.hbm.xml</value>
<value>mappings/Event.hbm.xml</value>
<value>mappings/User.hbm.xml</value>
<value>mappings/Registration.hbm.xml</value>
<value>mappings/RegisteredEvent.hbm.xml</value>
<value>mappings/Table.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<!-- Add DAOs here -->
<bean id="slotDAO" class="com.winterwar.dao.hibernate.SlotDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="eventDAO" class="com.winterwar.dao.hibernate.EventDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="conventionDAO" class="com.winterwar.dao.hibernate.ConventionDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="registeredEventDAO" class="com.winterwar.dao.hibernate.RegisteredEventDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="registrationDAO" class="com.winterwar.dao.hibernate.RegistrationDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="userDAO" class="com.winterwar.dao.hibernate.UserDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="tableDAO" class="com.winterwar.dao.hibernate.TableDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- Add Managers here -->
<!-- Convention Manager -->
<bean id="conventionManagerTarget" class="com.winterwar.service.impl.ConventionManagerImpl">
<property name="DAO">
<ref local="conventionDAO"/>
</property>
</bean>
<bean id="conventionManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="conventionManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- Slot Manager -->
<bean id="slotManagerTarget" class="com.winterwar.service.impl.SlotManagerImpl">
<property name="DAO">
<ref local="slotDAO"/>
</property>
</bean>
<bean id="slotManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="slotManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- Event Manager -->
<bean id="eventManagerTarget" class="com.winterwar.service.impl.EventManagerImpl">
<property name="DAO">
<ref local="eventDAO"/>
</property>
</bean>
<bean id="eventManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="eventManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- User Manager -->
<bean id="userManagerTarget" class="com.winterwar.service.impl.UserManagerImpl">
<property name="DAO">
<ref local="userDAO"/>
</property>
</bean>
<bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="userManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- Registration Manager -->
<bean id="registrationManagerTarget" class="com.winterwar.service.impl.RegistrationManagerImpl">
<property name="DAO">
<ref local="registrationDAO"/>
</property>
</bean>
<bean id="registrationManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="registrationManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- Registered Event Manager -->
<bean id="registeredEventManagerTarget" class="com.winterwar.service.impl.RegisteredEventManagerImpl">
<property name="DAO">
<ref local="registeredEventDAO"/>
</property>
</bean>
<bean id="registeredEventManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="registeredEventManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- Table Manager -->
<bean id="tableManagerTarget" class="com.winterwar.service.impl.TableManagerImpl">
<property name="DAO">
<ref local="tableDAO"/>
</property>
</bean>
<bean id="tableManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="tableManagerTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
The actual DAO:
package com.winterwar.dao.hibernate;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.winterwar.base.Slot;
import com.winterwar.dao.SlotDAO;
public class SlotDAOHibernate extends HibernateDaoSupport implements SlotDAO {
private Log log = LogFactory.getLog(SlotDAO.class);
public Slot get(Integer slotID) {
return (Slot) getHibernateTemplate().get(Slot.class, slotID);
}
public List getAll() {
return (List) getHibernateTemplate().find("from Slot order by slotName");
}
public void remove(Integer slotID) {
Object slot = getHibernateTemplate().load(Slot.class, slotID);
getHibernateTemplate().delete(slot);
}
public void save(Slot slot) {
getHibernateTemplate().saveOrUpdate(slot);
if (log.isDebugEnabled()) {
log.debug("userId set to: " + slot.getSlotID());
}
}
public List getByConID(Integer conID) {
return getHibernateTemplate().find("from Slot where conID = " + conID.intValue() + " order by slotName");
}
}
And the mapping file for my "Slot" class:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.winterwar.base.Slot" table="Slot">
<id name="slotID" column="slotID" unsaved-value="0">
<generator class="increment" />
</id>
<property name="startDate" column="startDate" not-null="true"/>
<property name="endDate" column="endDate" not-null="true"/>
<property name="slotName" column="slotName" not-null="true"/>
<property name="conID" column="conID" not-null="true"/>
<property name="isLocked" column="isLocked" not-null="true"/>
<property name="usedTables" formula="(select count(*) from Event where Event.slotID = slotID and Event.status = 3 and Event.tableID is not NULL and Event.tableID != 0)"/>
<property name="totalTables" formula="(select count(*) from Event_Table where Event_Table.conID = conID)"/>
<property name="eventCount" formula="(select count(*) from Event where Event.slotID = slotID and Event.status = 3)"/>
<set name="events" lazy="true" inverse="true" order-by="eventName">
<key column="slotID" />
<one-to-many class="com.winterwar.base.Event" />
</set>
</class>
</hibernate-mapping>