Actually, I have one scenario where I need to insert master record and update already existed bunch of child records with new master record id. For this I have called saveOrUpdate() to save master record and get the ID, which was generated and called executeUpdate() to update my bunch of records by update statement.
The problem is, first it executes my update statements, where it tried to update all child records with new parent key. As the new parent record was not inserted it throws db exception.
saveOrUpdate() is being called after my update statements.
How can I synchronize these two calls, so that I can insert my new parent record with saveOrUpdate() first and update my all child questions with executeUpdate(), which takes hql update statement.
Please provide your answers / resolutions if any one face this issue.
Attaching the code snippet for clear understanding
Transactional(propagation = Propagation.SUPPORTS, rollbackFor = SFFDatabaseException.class, isolation = Isolation.READ_UNCOMMITTED) public Response editQuestion(parentRequest parentRequest) throws SFFDatabaseException { parent.setXXXX("XXXX"); parent.setXXXX("XXXX"); parent.setXXXX("XXXX"); sessionFactory.getCurrentSession().saveOrUpdate(parent); parentId = parent.getId(); Query updateQuestion = sessionFactory.getCurrentSession().createQuery("update child set parentId=:nqid"); updateQuestion.setParameter("nqid", parentId); updateQuestion.executeUpdate();
Here first it executes updateQuestion.executeUpdate(); then sionFactory.getCurrentSession().saveOrUpdate(parent); I am getting parent id in parentId but it is not saving the parent record first, so I am getting foreign key exception.
Thanks, Vikram.N
|