Spring CrudRepository异常 [英] Spring CrudRepository exceptions

查看:135
本文介绍了Spring CrudRepository异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Spring Data CrudRepository ,它处理数据库上的CRUD操作。

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

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

}

用户是我的数据库的用户表的实体。 CrudRepository 将以下操作添加到存储库:

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)

  • 保存(用户用户)

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

文档,删除并查找操作抛出 IllegalArgumentException 以防给定的id为null,而save操作不会抛出任何异常。

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.

问题是CrudRepository的javadoc没有提到这些操作抛出的其他异常。例如,它没有告诉 delete(String ID)操作抛出 EmptyResultDataAccessException ,以防提供的ID是数据库中不存在。

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.

保存(用户用户)操作的javadoc中,不清楚哪些例外是如果您插入一个新的用户,它会破坏一个数据完整性约束(在唯一字段和外键上)。此外,它不会警告您是否正在编写新用户或现有用户:它只是创建一个新用户或覆盖(如果存在)(因此它是一个插入+更新操作)。

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).

在企业应用程序中,我应该能够捕获操作可以抛出的每个抛出异常,我应该在操作的javadoc中读到它。

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.

你知道吗?关于CrudRepository异常的文档?

Do you know any clear documentation about CrudRepository exceptions?

谢谢

推荐答案

Spring已经构建-in异常转换机制,以便将JPA持久性提供程序抛出的所有异常转换为Spring的DataAccessException - 对于使用@Repository(或已配置)注释的所有bean。

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).

有四个主要组 -


  • NonTransientDataAccessException - 除非纠正异常的原因,否则这些异常会导致相同操作的重试失败。因此,如果您传递非现有ID,则除非数据库中存在id,否则它将失败。

  • 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 - 这些是前一个的反面 - 可恢复的异常 - 在一些恢复步骤之后。 API文档中的更多详细信息

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相关的异常,例如在尝试处理格式不正确的脚本时。

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

TransientDataAccessException - 这些都是例外当没有任何明确的步骤可以恢复时,例如当数据库超时时,你会在几秒后重试。

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.

那说,理想的地方是查找有关所有异常的文档 - 在API本身 - 只需浏览 DataAccessException

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

这篇关于Spring CrudRepository异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆