Hibernate-core 5.2.18

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?

Hi @smart_hack

In Hibernate 5.2.x, basically if a transaction gets marked for rollback its pretty much game over. You will need to restart the transaction from scratch. This is due to the fact Hibernate adheres to strict JPA compliance for this behavior.

In Hibernate 5.3.x, a set of configurable compliance options were introduced to allow users who were trying to upgrade to 5.2.x to return to some previous JPA non-compliant behavior.

So in short, if you can upgrade to one of the latest 5.3.x releases, you should be able to return to that same behavior you had previously since 5.3 disables JPA compliance by default but this may change in future releases, so check the release notes to be sure.

The configuration option to explicitly keep the old behavior is:

hibernate.jpa.compliance.transaction=false
1 Like