Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "persistence exception translation" for @Repository beans

Tags:

I was reading Spring with annotation part and I came across @Repositoryannotation
I read that @Repository beans differ from @Component beans in the sense that they are eligible for persistence exception translation.

Can somebody please elaborate what is meant by persistence exception translation?

like image 441
Anand Avatar asked Aug 30 '12 19:08

Anand


People also ask

What is Exception translation?

Exception translation (or exception conversion) is the process of converting one type of exception into another. Exception translation is typically applied if exceptions from third party libraries do not fit into your application.

What is the use of PersistenceExceptionTranslationPostProcessor?

Class PersistenceExceptionTranslationPostProcessorTranslates native resource exceptions to Spring's DataAccessException hierarchy. Autodetects beans that implement the PersistenceExceptionTranslator interface, which are subsequently asked to translate candidate exceptions.

What is DataAccessException in spring?

Class DataAccessExceptionThis exception hierarchy aims to let user code find and handle the kind of error encountered without knowing the details of the particular data access API in use (e.g. JDBC). Thus, it is possible to react to an optimistic locking failure without knowing that JDBC is being used.


2 Answers

Persistence Exception Translation is the process of converting low level persistence exceptions into high level Spring exceptions.

From the SpringSource Site:

Common data access exceptions. Spring can wrap exceptions from your O/R mapping tool of choice, converting them from proprietary (potentially checked) exceptions to a common runtime DataAccessException hierarchy. This allows you to handle most persistence exceptions, which are non-recoverable, only in the appropriate layers, without annoying boilerplate catches/throws, and exception declarations. You can still trap and handle exceptions anywhere you need to. Remember that JDBC exceptions (including DB specific dialects) are also converted to the same hierarchy, meaning that you can perform some operations with JDBC within a consistent programming model.

One of the major benefits of this is that exceptions are turned into Runtime Exceptions, in effect you are not required to add the throws declaration to your methods signature.

http://static.springsource.org/spring/docs/2.5.x/reference/orm.html

like image 55
Kevin Bowersox Avatar answered Oct 28 '22 23:10

Kevin Bowersox


It provides a consistent exception hierarchy regardless of the database type or persistence methodology/technology you're using.

You get the same exceptions for the same types of errors regardless of whether you're using Oracle vs MySQL or JPA vs JDBC.

Take a look at the SQLErrorCodeSQLExceptionTranslator and sql-error-codes.xml.

sql-error-codes.xml is particularly interesting as you can see all the various vendor-specific error codes and what exception in the hierarchy they map to.

like image 33
dustin.schultz Avatar answered Oct 29 '22 00:10

dustin.schultz