Hi,
I have a requirement to use JTA transacation manager, batch commit and commit the batch even if ConstraintViolationException is thrown.
Currently I am getting an exception during the commit of the batch, and the object identifier show it is the same object that is causing the unique constraint violation.
"Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for HierarchyNode instance with identifier: 42884; nested exception is net.sf.hibernate.StaleObjectStateException: "
snip*
Code:
Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
try
{
session.save(node);
session.flush();
}
catch(ConstraintViolationException e)
{
try
{
session.evict(node);
session.cancelQuery();
node.setID(null);
node.setVersion(null);
}
catch (HibernateException e1){e1.printStackTrace();}
}
Is it possible with hibernate to bind batches insert in one transaction, the first insert succeed, the second insert violate unique constraint and I go ahead and commit anyway, because I want first insert to go into the db. In the code snippet I try to evict, cancelQuery, setId and version to null to the object causing the problem. But still get the StaleObjectStateException on commit. How can I achieve this with hibernate?
Thank you.
Code: