mavarazy
mavarazy

Reputation: 7735

Is there a way to translate HibernateException to something else, then DataAccessException in Spring

I am trying to make more sense out of constraint violation errors, happening during transaction persistence in Hibernate.

The only way I see how to interfere with HibernateExceptions is through PersistenceExceptionTranslator in spring,

package org.springframework.dao.support;

import org.springframework.dao.DataAccessException;

public interface PersistenceExceptionTranslator {

    DataAccessException translateExceptionIfPossible(RuntimeException ex);

}

which translates Hibernate exceptions to DataAccessExceptions, but that means I'll need to inherit my exception hierarchy from DataAccessExceptions.

Is there any other natural way to do this, without introducing DataAceessExceptions in Exception inheritance hierarchy?

Upvotes: 0

Views: 426

Answers (1)

Flying Dumpling
Flying Dumpling

Reputation: 1324

You can catch HibernateException and rethrow it as a checked exception from your repository method. But then you will have to deal with the exception in the upper layers, but you also can simply keep throwing it outside your code layer.

Upvotes: 1

Related Questions