Hi,
I am using the OpenSessionInViewFilter to implement the session-per-request pattern, so that I can lazily load collections, but it is giving an error consistently:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.domain.Environment.applications - no session or session was closed
My DAOs use Spring's HibernateTemplate. Does this close the session after being called? My DAO method being called is:
public Environment findById(long environmentId)
{
List envs = getHibernateTemplate().find( "from Environment where id = ?", new Long(environmentId) );
if ( envs.size() > 0 )
return (Environment)envs.get(0);
else
return null;
}
The Environment object returned has a collection of Application objects, and this is the collection I want to traverse lazily.
I am also using HibernateTransactionManager, to further complicate things, and have the above method wrapped in a transaction - would this affect lazy loading or Session lifecycle?
The Hibernate mappings are as follows:
Environment:
<class name="com.domain.Environment" table="ENVIRONMENT">
<id name="environmentId" column="environment_id" type="long">
<generator class="sequence">
<param name="sequence">environment_id_sq</param>
</generator>
</id>
<property name="name" column="name" />
<property name="createdOn" column="created_on" />
<property name="createdBy" column="created_by" />
<property name="updatedOn" column="updated_on" />
<property name="updatedBy" column="updated_by" />
<set name="applications" table="APPLICATION" lazy="true" inverse="true">
<key column="ENVIRONMENT_ID" />
<one-to-many class="com.domain.Application" />
</set>
</class>
Application:
<class name="com.geai.b2b.domain.Application" table="APPLICATION">
<id name="applicationId" column="application_id" type="long">
<generator class="sequence">
<param name="sequence">application_id_sq</param>
</generator>
</id>
<property name="name" column="name" />
<property name="createdOn" column="created_on" />
<property name="createdBy" column="created_by" />
<property name="updatedOn" column="updated_on" />
<property name="updatedBy" column="updated_by" />
<many-to-one name="environment" class="com.domain.Environment" column="ENVIRONMENT_ID"></many-to-one>
<set name="applicationDevices" lazy="true" inverse="true">
<key column="APPLICATION_ID" />
<one-to-many class="com.domain.ApplicationDevice" />
</set>
</class>
My OpenSessionInViewFilter is configured like this:
<filter>
<filter-name>osivFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>osivFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
with ContextLoaders configured in web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
and also in struts-config.xml:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
Versions: Hibernate3, Spring 1.2.
Thanks in advance for any help!
-Andrew
_________________ 1-800-flowers.com? How on earth do you reach these people?
|