Results 1 to 5 of 5

Thread: Transaction Problem using Spring+Hibernate

  1. #1
    Join Date
    Aug 2004
    Posts
    6

    Default Transaction Problem using Spring+Hibernate

    Hi,
    I have encountered problems using declarative transaction demarcation. I have a parent-child class "binded" using foreign key in Hibernate. In my business manager class, I have a save() method to save both the parent and child object. However, I realised that the Hibernate session closes (with a transaction completion message) after the parent class is being saved. To confirm that the transaction did not propagate to the child, i through an exception at the child's DAO class.

    I am quite sure that there are some attribute that I did not set for my dataSource bean or transaction manager. Below is a small segment of my application context. Appreciate any help offered. Tks.

    Code:
    	<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    	  <property name="jndiName"><value>MY_DS</value></property>
    	</bean>
    	
    	<bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="myDataSource"/></property>
    		<property name="mappingResources">
    		  <list>
    		    <value>User.hbm</value>
    		    <value>OneOffFA.hbm</value>
    		  </list>
    		</property>
    		<property name="hibernateProperties">
    		  <props>
    		    <prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
    		    <prop key="hibernate.show_sql">true</prop>
    		  </props>
    		</property>
    	</bean>
    	<bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="mySessionFactory"/></property>
    	</bean>
    	<bean id="oneOffFADelegateTarget" class="delegate.financial.OneOffFADelegate">
    		<property name="workflowSvc"><ref bean="workflowSvc"/></property>
    		<property name="paramMgr"><ref bean="paramMgr"/></property>
    		<property name="txnCounter"><ref bean="transRefCounter"/></property>
    		<property name="dao"><ref local="oneOffFADao"/></property>
    	</bean>
    	
    	<bean id="oneOffFADelegate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"> <ref bean="myTransactionManager"/></property>
    		<property name="target"> <ref local="oneOffFADelegateTarget"/> </property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    	<bean id="oneOffFADao" class="iwads.dao.hibernate.OneOffFADaoImpl">
    		<property name="sessionFactory"><ref bean="mySessionFactory"/></property>
    	</bean>

  2. #2
    Join Date
    Aug 2004
    Location
    Warsaw, Poland
    Posts
    33

    Default Re: Transaction Problem using Spring+Hibernate

    Quote Originally Posted by aikkee
    Hi,
    I have encountered problems using declarative transaction demarcation. I have a parent-child class "binded" using foreign key in Hibernate. In my business manager class, I have a save() method to save both the parent and child object. However, I realised that the Hibernate session closes (with a transaction completion message) after the parent class is being saved. To confirm that the transaction did not propagate to the child, i through an exception at the child's DAO class.
    If I understand correctly You tries to save parent and child separatelly. This is not proper approach since Hibernate usually handles this cascading operations from parent to child. Please show Your mapping documents and code in Your business save method.
    I guess You saves Parent, thus your child is saved because of hibernate cascading and next, You tries to save (second time) child.

    Artur

  3. #3
    Join Date
    Aug 2004
    Posts
    6

    Default

    Hi Artur,
    You are right. Here's my mapping & code, pls advise. tks.
    Code:
    <hibernate-mapping package="domain">
    	<class name="domain.Master" table="MASTER" schema="apps">
    		<id name="transactionNo" type="java.lang.String" column="TRANS_NO"> 
    			<generator class="assigned" />
    		</id>
    		<property name="remarks" column="REM" type="java.lang.String"/>
    	</class>
    	<class name="domain.OneOffFA" table="ONE_OFF_FA" schema="apps">
    		<id name="transactionNo" column="CLAIM_NO" type="java.lang.String">
    			<generator class="foreign">
    				<param name="property">master</param>
    			</generator>
    		</id>
    		<property name="type" column="TYPE" type="java.lang.String" not-null="true" />
    		<property name="amount" column="AMT" type="double"  not-null="true"/>
    		<property name="reason" column="REASON" type="java.lang.String" not-null="false"/>
    		<one-to-one name="master" cascade="all"  outer-join="true"/>
    	</class>
    </hibernate-mapping>

    Code:
    	public void save&#40;OneOffFA aFA&#41; throws DataAccessException
    	&#123;
    		String claimNo = &#40;String&#41;getHibernateTemplate&#40;&#41;.save&#40;aFA.getSchemeApplication&#40;&#41;&#41;;
    		getHibernateTemplate&#40;&#41;.save&#40;aFA, claimNo&#41;;
    		
    	&#125;

  4. #4
    Join Date
    Aug 2004
    Location
    Warsaw, Poland
    Posts
    33

    Default

    Quote Originally Posted by aikkee
    Hi Artur,

    Code:
    <one-to-one name="master" cascade="all" outer-join="true"/>
    
      String claimNo = &#40;String&#41;getHibernateTemplate&#40;&#41;.save&#40;aFA.getSchemeApplication&#40;&#41;&#41;;
      getHibernateTemplate&#40;&#41;.save&#40;aFA, claimNo&#41;;
    As I said. All Your operations are cascaded. Replace Your save method with:
    Code:
    getHibernateTemplate&#40;&#41;.save&#40;aFA,&#41;;
    Should work.

    Artur

  5. #5
    Join Date
    Aug 2004
    Posts
    6

    Default Transaction Problem using Spring+Hibernate

    Hi Artur,
    I tried, it will insert into OneOffFA but tries to update the Master records. So it ends up that I have a OneOffFA record but no master record. Is there any thing wrong with my mapping?

    Aik Kee

Similar Threads

  1. Replies: 2
    Last Post: Aug 31st, 2005, 12:37 PM
  2. Replies: 0
    Last Post: Jun 6th, 2005, 06:22 AM
  3. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •