Java – Does Spring JPA throw an error if save function is unsuccessful


I would like to know if and what type of error is thrown by Spring JPA when trying to save an object to a database. The says to look at However, I can not seem to find what type of error is thrown, if any at all. If no error is thrown, how do I go about checking if the save was successful?

Any insight would be much appreciated.

Best Solution

Spring Data Repositories throw Spring's DataAccessExceptions that are structured to allow you to catch exceptions based on certain error scenarios. E.g. a DataIntegrityViolationException is thrown to indicate foreign key violations.

The original exception is contained inside the exception being thrown so that you can get to it if you need to. By throwing a persistence technology agnostic exception, the repository clients don't get polluted with technology specific exceptions (e.g. Hibernate ones, JPA ones, MongoDB ones) so that you can exchange the repository implementation without breaking clients.