We are using Spring 5.1.6.RELEASE and Hibernate 5.2.18.Final versions for our application
In below code snippet, the flush method after delete marks the transaction as rollbackOnly internally (TransactionImpl.setRollbackOnly()). Which seems to be happening because delete is resulting into an exception due to child record’s existence. The code catches the exception as the requirement is to make some changes for the entity when it cannot be deleted. And it does get caught.
The problem is: The hibernate transaction is still marked as rollBackOnly. So on returning back from the method, when my main transaction goes for commit, it gives me error :
java.lang.RuntimeException: org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit
org.springframework.orm.hibernate5.HibernateTemplate hibernateTemplate;
public void method1(ID id)
{
doStuff();
try
{
//Trying to delete entity record
hibernateTemplate.delete(id);
hibernateTemplate.flush();
}
catch(Exception e)
{
//Changing id entity to mark a flag
id.changeFlag("Y");
hibernateTemplate.saveOrUpdate(id);
}
}
In Hibernate 3.X version this code snippet was working fine, however it’s not working in Hibernate 5.2.18 due to that rollBackOnly flag.
Can anyone help me in overcoming this issue?