Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring CrudRepository exceptions

I have this Spring Data CrudRepository which handles the CRUD operations on a DB.

@Repository public interface IUserRepository extends CrudRepository<User, String> {  } 

User is the Entity of a User table of my DB. CrudRepository adds namely the following operations to the repository:

  • delete(String ID)
  • findOne(String ID)
  • save(User user)

As stated in the documentation, the delete and find operations throw IllegalArgumentException in case the given id is null while the save operation doesn't throw any exception.

The problem is that the javadoc of the CrudRepository makes no mention about the other exceptions thrown by these operations. For example it doesn't tell that the delete(String ID) operation throws a EmptyResultDataAccessException in case the provided ID is nonexistent in the DB.

In the javadoc of the save(User user) operation it's not clear which exceptions are thrown in case you insert a new User which breaks one data integrity constraint (on unique fields and foreign keys). Moreover it doesn't warn you whether you are writing a new or existent User: it just creates a new User or overwrites if existent (so it's a Insert + Update operation).

In a enterprise application I should be able to catch every throwable exception an operation can throw and I should read about that in the operation's javadoc.

Do you know any clear documentation about CrudRepository exceptions?

like image 473
gvdm Avatar asked Apr 27 '14 15:04

gvdm


People also ask

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What are the exceptions in Spring boot?

Exception HandlerThe @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.

How does Spring handle Hibernate exceptions?

Hibernate provides better handle than the JDBCException . Developers can use the try and catch block to handle the exceptions. Put the line of code that may cause an exception in a try block and according to that exception put the exception handler in the catch block.

How do you handle exceptions in JPA?

Application Exceptions checked exceptions basically we are using business validation and business rules. System Exceptions are runtime exceptions, so that if any runtime excpetion happend ejb container will interfer and convert the runtime exception as remote exception. All jpa exceptions are runtime exceptions only.


1 Answers

Spring has built-in exception translation mechanism, so that all exceptions thrown by the JPA persistence providers are converted into Spring's DataAccessException - for all beans annotated with @Repository (or configured).

There are four main groups -

  • NonTransientDataAccessException - these are the exceptions where a retry of the same operation would fail unless the cause of the Exception is corrected. So if you pass non existing id for example, it will fail unless the id exists in the database.

  • RecoverableDataAccessException - these are the "opposite" of the previous one - exceptions which are recoverable - after some recovery steps. More details in the API docs

  • ScriptException - SQL related exceptions, when trying to process not well-formed script for example.

  • TransientDataAccessException - these are the exception when recovery is possible without any explicit step, e.g. when there is a timeout to the database, you are retrying after few seconds.

That said, the ideal place to find documentation about all exceptions - is in the API itself - just go through the hierarchy of DataAccessException.

like image 67
vtor Avatar answered Sep 22 '22 23:09

vtor