Java – spring crudrepository exception

I have this spring data crudrepository to handle CRUD operations on DB

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

}

Users are entities of my database user tables Crudrepository adds the following operations to the repository:

>Delete (string ID) > findone (string ID) > Save (user)

As described in the documentation, the delete and find operations will throw illegalargumentexception to prevent the given ID from being null, and the save operation will not throw any exceptions

The problem is that the Javadoc of crudrepository does not mention other exceptions thrown by these operations For example, if the provided ID does not exist in the DB, it will not indicate that the delete (string ID) operation will throw an emptyresultdataaccessexception

In the Javadoc of save (user) operation, it is not clear what exceptions will be thrown when inserting a new user (on unique fields and foreign keys) that breaks a data integrity constraint In addition, it does not warn you whether you are writing a new or existing user: it just creates a new user or overlay (if any) (so it is an insert update operation)

In an enterprise application, I should be able to catch every exception that an operation can throw, and I should read it in the Javadoc of the operation

Do you know explicit documentation about crudrepository exceptions?

thank you

Solution

Spring has a built-in exception conversion mechanism, so all exceptions thrown by the JPA persistence provider are converted to spring's dataaccessexception - for all beans annotated with @ repository (or configuration)

There are four main groups –

>Nontransientdataaccessexception – retry of the same operation will fail unless the cause of the exception is corrected Therefore, if you pass a non-existing ID, for example, it will fail unless it exists in the database. > Recoverabledataaccessexception - these are the "opposite" of the previous - recoverable exceptions - after some recovery steps More details in API documentation > scriptexception – SQL related exceptions when trying to handle scripts that are not well formed. > Transientdataaccessexception – these are exceptions that can be recovered without any explicit steps When the database times out, you will try again in a few seconds

That is, the ideal place to find documentation about all exceptions is the API itself - just traverse the hierarchy of dataaccessexception

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>