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.