Announcement Announcement Module
Collapse
No announcement yet.
Transaction Problem using Spring+Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Re: Transaction Problem using Spring+Hibernate

    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

    Comment


    • #3
      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;

      Comment


      • #4
        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

        Comment


        • #5
          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

          Comment

          Working...
          X