Reputation: 24411
I'm using spring-data-jpa to read/write to my DB. I've read through the documentation for spring-data and the one thing that I can't seem to locate is how it handles exceptions.
I am looking to catch any type of exception thrown by a repo action. Is there a way of doing this short of just wrapping the individual repo action in a try/catch and catching any RuntimeException? I'd rather avoid that as that is an even larger catch-all than I'd like.
Does spring-data wrap all exceptions in its own SpringDataException() or something similar? I've looked through the spring-data-commons jar as well as the spring-data-jpa jar and didn't find anything applicable.
Upvotes: 4
Views: 2661
Reputation: 19000
Does spring-data wrap all exceptions in its own SpringDataException() or something similar
Yes, sort of. spring-data(-jpa) enable exception translation from JPA exceptions to Spring’s DataAccessException hierarchy:
From spring-data-jpa reference implementation:
...Beyond that, it activates persistence exception translation for all beans annotated with @Repository to let exceptions being thrown by the JPA persistence providers be converted into Spring’s DataAccessException hierarchy.
From Spring reference implementation:
Spring provides a convenient translation from technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception. These exceptions wrap the original exception so there is never any risk that one might lose any information as to what might have gone wrong...
For more read about Spring DAO support
Upvotes: 4