-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: parent key not found, Save two connected objects
PostPosted: Tue May 25, 2010 10:19 am 
Newbie

Joined: Wed Mar 11, 2009 7:15 am
Posts: 13
My BO beans inherit from transactionProxyBean
Code:
<bean id="txManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <bean id="transactionProxyBean" abstract="true"
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager" ref="txManager" />
      <property name="proxyTargetClass">
         <value>true</value>
      </property>
      <property name="transactionAttributeSource">
         <bean
            class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
      </property>
   </bean>
   
   <bean id="orderGeneralDao" class="pl.diagno.dao.impl.OrderGeneralDAOImpl">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>
   
      <bean id="orderBo" parent="transactionProxyBean">
      <property name="target">
         <bean class="pl.diagno.bo.impl.OrderBOImpl">
            <property name="personDAO" ref="personDao" />
            <property name="testDAO" ref="testDao" />
            <property name="orderGeneralDAO" ref="orderGeneralDao" />
            <property name="orderDetailsDAO" ref="orderDetailsDao" />
         </bean>
      </property>
   </bean>

In OrderDetails is a reference @ManyToOne to OrderGeneral.

The problem is that I can not save OrderGeneral and referenced on it OrderDetails in one method/transaction. This is my method
Code:
   public void saveOrder(long personId, long clinicId, long testsId[]){
      
      OrderGeneral orderGeneral = new OrderGeneral();
      orderGeneral.setPerson(personDAO.load(personId));
      orderGeneralDAO.getHibernateTemplate().getSessionFactory().getCurrentSession().beginTransaction();
      orderGeneralDAO.save(orderGeneral); // save orderGeneral

      orderGeneralDAO.getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
      for(int i = 0; i<testsId.length; i++){
         OrderDetails orderDetails = new OrderDetails();
         orderDetails.setOrderGeneral(orderGeneral); //set orderGeneral
         orderDetails.setTest(testDAO.load(testsId[i]));
         orderDetailsDAO.save(orderDetails); //save orderDetails
      }
   }

Exception:
Code:
java.sql.BatchUpdateException: ORA-02291: integrity constraint (DIAGNO.ORDERDETAILS_ORDERGENERAL_FK) violated - parent key not found

I tried to move writing orderDetails to the another metod but it doesn't help.
Code:
   
//   @Transactional(propagation = Propagation.REQUIRED)
   public void saveOrder(long personId, long clinicId, long testsId[]){
      
      OrderGeneral orderGeneral = new OrderGeneral();
      orderGeneral.setPerson(personDAO.load(personId));
//      orderGeneralDAO.getHibernateTemplate().getSessionFactory().getCurrentSession().beginTransaction();
      orderGeneralDAO.save(orderGeneral);
//      orderGeneralDAO.getHibernateTemplate().flush();
//      orderGeneralDAO.getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
      saveOrderDetails(orderGeneral, testsId);
   }
   
//   @Transactional(propagation = Propagation.REQUIRES_NEW)
   public void saveOrderDetails(OrderGeneral orderGeneral, long testsId[]){
      for(int i = 0; i<testsId.length; i++){
         OrderDetails orderDetails = new OrderDetails();
         orderDetails.setOrderGeneral(orderGeneral);
         orderDetails.setTest(testDAO.load(testsId[i]));
         orderDetailsDAO.save(orderDetails);
      }
   }

Commented lines show my tests but nothing help.
Code:
OrderDetailsDAO has
    @Transactional(propagation = Propagation.REQUIRED)
    public void save(final OrderDetails entity) {
        hibernateTemplate.saveOrUpdate(entity);
        hibernateTemplate.flush();
    }

After the exception, there is new recodr OrderGeneral in database, but there is no OrderDetails.

Please for help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.