I was reading Spring with annotation part and I came across @Repository
annotation
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?
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With