Loumeister
Aug 16th, 2004, 09:32 PM
I had this working last week under a slightly different configuration and now I can't repeat what I did. Basically, I am trying to lazy initialize collections and it's complaining : "Failed to lazily initialize a collection - no session or session was closed " Before, Spring was taking care of this for me fine, even when I used "getHibernateTemplate().find()". Can someone take a look? Greatly appreciated...I just can't look at this anymore today.
Gracious,
Lou
Here's the test case:
public void testUpdateClaim() throws DaoException {
Long iRslt = null;
ClmClaim clm = claimDao.findClmClaim(new Long(1096));
ClmClaimExposure exp = createExposure();
exp.setClaim(clm);
clm.addToExposureSet(exp); //<<<<<<<< blows up on this line
expDao.save(exp);
iRslt = exp.getId();
System.out.println("+++++++++ ID saved: " + iRslt + "+++++++++++");
}
The find method I have written is as follows:
Session s = SessionFactoryUtils.getSession(getSessionFactory() , false);
try {
Object object = s.load(getReferenceClass(), id);
//return getHibernateTemplate().load(getReferenceClass(), id);
return object;
} catch (DataAccessException e) {
throw new DaoException(e);
} catch (HibernateException he) {
throw SessionFactoryUtils.convertHibernateAccessExceptio n(he);
}
And my applicationCtx:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--Below is the following that you would use when testing outside the J2EE container-->
<bean id="clmDataSource"
class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@xxx.com:1521:nmad</value>
</property>
<property name="username">
<value>xxx</value>
</property>
<property name="password">
<value>xxx</value>
</property>
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFact oryBean">
<property name="dataSource">
<ref local="clmDataSource"/>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--Below is the following that you would use when testing outside the J2EE container (no CMT)-->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate.HibernateTransac tionManager">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<!--Use an AOP interceptor to attach the Hibernate session to CMT for session-per-
- transaction scoping. This way one hibernate session will live with the transaction.-->
<bean id="myHibernateInterceptor"
class="org.springframework.orm.hibernate.HibernateInterce ptor">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="clmClaimDaoTarget" singleton="false"
class="com.mitchell.services.technical.claim.dao.spring.C lmClaimHibernateDao">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<bean id="clmClaimDao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.mitchell.services.technical.claim.dao.ClmClaim Dao</value>
</property>
<property name="interceptorNames">
<list>
<value>myHibernateInterceptor</value>
<value>clmClaimDaoTarget</value>
</list>
</property>
</bean>
<bean id="clmClaimExposureDaoTarget" singleton="false"
class="com.mitchell.services.technical.claim.dao.spring.C lmClaimExposureHibernateDao">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<bean id="clmClaimExposureDao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.mitchell.services.technical.claim.dao.ClmClaim ExposureDao</value>
</property>
<property name="interceptorNames">
<list>
<value>myHibernateInterceptor</value>
<value>clmClaimExposureDaoTarget</value>
</list>
</property>
</bean>
<bean id="claimDaoManager"
class="com.mitchell.services.technical.claim.dao.ClaimDao Mgr">
<property name="clmClaimDao">
<ref local="clmClaimDao"/>
</property>
<property name="clmClaimExposureDao">
<ref local="clmClaimExposureDao"/>
</property>
</bean>
<!-- We don't need to do this since Spring is smart about going JTA if
- it exists, which in our case does when implementing with CMT.
- You would only need this for Spring-based CMT.
-->
<!--<bean id="myClaimService"
class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<property name="transactionManager">
<ref bean="myTransactionManager"/>
</property>
<property name="target">
<ref bean="claimDaoManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<!-#-<prop key="someOtherBusinessMethod">
PROPAGATION_MANDATORY</prop>-#->
</props>
</property>
</bean>-->
</beans>
Hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="ClmSessionFactory">
<property name="dialect">
net.sf.hibernate.dialect.Oracle9Dialect
</property>
<property name="hibernate.max_fetch_depth">3</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<property name="hibernate.jdbc.use_streams_for_binary">true</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.query.substitutions">
true 'T', false 'F', yes 'Y', no 'N'
</property>
<!--
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">
javax.transaction.UserTransaction
</property>
<property name="hibernate.transaction.manager_lookup_class">
net.sf.hibernate.transaction.WeblogicTransactionMa nagerLookup
</property>
-->
<mapping resource="com/mitchell/services/technical/claim/dao/vo/ClmClaim.hbm.xml" />
<mapping resource="com/mitchell/services/technical/claim/dao/vo/ClmClaimExposure.hbm.xml" />
</session-factory>
</hibernate-configuration>
And my mappings:
<hibernate-mapping package="com.mitchell.services.technical.claim.dao.vo">
<class name="ClmClaim" table="CLM_CLAIM">
<id
name="id"
type="java.lang.Long"
column="CLAIM_ID"
>
<generator class="sequence">
<param name="sequence">CLAIM_ID_SEQ</param>
</generator>
</id>
<version name="tcn" column="TCN" type="java.lang.Long" unsaved-value="null"/>
<set
inverse="true"
lazy="true"
name="exposureSet"
cascade="save-update"
>
<key column="CLAIM_ID" />
<one-to-many class="ClmClaimExposure" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.mitchell.services.technical.claim.dao.vo">
<class name="ClmClaimExposure" table="CLM_CLAIM_EXPOSURE" lazy="true">
<id
name="id"
type="java.lang.Long"
column="CLAIM_EXPOSURE_ID"
>
<generator class="sequence">
<param name="sequence">EXPOSURE_ID_SEQ</param>
</generator>
</id>
<version name="tcn" column="TCN" type="java.lang.Long" unsaved-value="null"/>
<many-to-one
class="ClmClaim"
name="claim"
not-null="true"
>
<column name="CLAIM_ID" />
</many-to-one>
</class>
</hibernate-mapping>
Gracious,
Lou
Here's the test case:
public void testUpdateClaim() throws DaoException {
Long iRslt = null;
ClmClaim clm = claimDao.findClmClaim(new Long(1096));
ClmClaimExposure exp = createExposure();
exp.setClaim(clm);
clm.addToExposureSet(exp); //<<<<<<<< blows up on this line
expDao.save(exp);
iRslt = exp.getId();
System.out.println("+++++++++ ID saved: " + iRslt + "+++++++++++");
}
The find method I have written is as follows:
Session s = SessionFactoryUtils.getSession(getSessionFactory() , false);
try {
Object object = s.load(getReferenceClass(), id);
//return getHibernateTemplate().load(getReferenceClass(), id);
return object;
} catch (DataAccessException e) {
throw new DaoException(e);
} catch (HibernateException he) {
throw SessionFactoryUtils.convertHibernateAccessExceptio n(he);
}
And my applicationCtx:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--Below is the following that you would use when testing outside the J2EE container-->
<bean id="clmDataSource"
class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@xxx.com:1521:nmad</value>
</property>
<property name="username">
<value>xxx</value>
</property>
<property name="password">
<value>xxx</value>
</property>
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFact oryBean">
<property name="dataSource">
<ref local="clmDataSource"/>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--Below is the following that you would use when testing outside the J2EE container (no CMT)-->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate.HibernateTransac tionManager">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<!--Use an AOP interceptor to attach the Hibernate session to CMT for session-per-
- transaction scoping. This way one hibernate session will live with the transaction.-->
<bean id="myHibernateInterceptor"
class="org.springframework.orm.hibernate.HibernateInterce ptor">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="clmClaimDaoTarget" singleton="false"
class="com.mitchell.services.technical.claim.dao.spring.C lmClaimHibernateDao">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<bean id="clmClaimDao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.mitchell.services.technical.claim.dao.ClmClaim Dao</value>
</property>
<property name="interceptorNames">
<list>
<value>myHibernateInterceptor</value>
<value>clmClaimDaoTarget</value>
</list>
</property>
</bean>
<bean id="clmClaimExposureDaoTarget" singleton="false"
class="com.mitchell.services.technical.claim.dao.spring.C lmClaimExposureHibernateDao">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<bean id="clmClaimExposureDao"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.mitchell.services.technical.claim.dao.ClmClaim ExposureDao</value>
</property>
<property name="interceptorNames">
<list>
<value>myHibernateInterceptor</value>
<value>clmClaimExposureDaoTarget</value>
</list>
</property>
</bean>
<bean id="claimDaoManager"
class="com.mitchell.services.technical.claim.dao.ClaimDao Mgr">
<property name="clmClaimDao">
<ref local="clmClaimDao"/>
</property>
<property name="clmClaimExposureDao">
<ref local="clmClaimExposureDao"/>
</property>
</bean>
<!-- We don't need to do this since Spring is smart about going JTA if
- it exists, which in our case does when implementing with CMT.
- You would only need this for Spring-based CMT.
-->
<!--<bean id="myClaimService"
class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<property name="transactionManager">
<ref bean="myTransactionManager"/>
</property>
<property name="target">
<ref bean="claimDaoManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<!-#-<prop key="someOtherBusinessMethod">
PROPAGATION_MANDATORY</prop>-#->
</props>
</property>
</bean>-->
</beans>
Hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="ClmSessionFactory">
<property name="dialect">
net.sf.hibernate.dialect.Oracle9Dialect
</property>
<property name="hibernate.max_fetch_depth">3</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<property name="hibernate.jdbc.use_streams_for_binary">true</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.query.substitutions">
true 'T', false 'F', yes 'Y', no 'N'
</property>
<!--
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">
javax.transaction.UserTransaction
</property>
<property name="hibernate.transaction.manager_lookup_class">
net.sf.hibernate.transaction.WeblogicTransactionMa nagerLookup
</property>
-->
<mapping resource="com/mitchell/services/technical/claim/dao/vo/ClmClaim.hbm.xml" />
<mapping resource="com/mitchell/services/technical/claim/dao/vo/ClmClaimExposure.hbm.xml" />
</session-factory>
</hibernate-configuration>
And my mappings:
<hibernate-mapping package="com.mitchell.services.technical.claim.dao.vo">
<class name="ClmClaim" table="CLM_CLAIM">
<id
name="id"
type="java.lang.Long"
column="CLAIM_ID"
>
<generator class="sequence">
<param name="sequence">CLAIM_ID_SEQ</param>
</generator>
</id>
<version name="tcn" column="TCN" type="java.lang.Long" unsaved-value="null"/>
<set
inverse="true"
lazy="true"
name="exposureSet"
cascade="save-update"
>
<key column="CLAIM_ID" />
<one-to-many class="ClmClaimExposure" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.mitchell.services.technical.claim.dao.vo">
<class name="ClmClaimExposure" table="CLM_CLAIM_EXPOSURE" lazy="true">
<id
name="id"
type="java.lang.Long"
column="CLAIM_EXPOSURE_ID"
>
<generator class="sequence">
<param name="sequence">EXPOSURE_ID_SEQ</param>
</generator>
</id>
<version name="tcn" column="TCN" type="java.lang.Long" unsaved-value="null"/>
<many-to-one
class="ClmClaim"
name="claim"
not-null="true"
>
<column name="CLAIM_ID" />
</many-to-one>
</class>
</hibernate-mapping>