Spring CrudRepository 异常 [英] Spring CrudRepository exceptions

查看:24
本文介绍了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> {

}

User 是我的数据库的用户表的实体.CrudRepository 即在存储库中添加以下操作:

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

  • 删除(字符串 ID)
  • findOne(字符串 ID)
  • 保存(用户用户)

documentation,删除和查找操作抛出 IllegalArgumentException 以防给定 id 为 null 而保存操作不抛出任何异常.

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 没有提及这些操作引发的其他异常.例如,如果提供的 ID 在 DB 中不存在,它不会告诉 delete(String ID) 操作会引发 EmptyResultDataAccessException.

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.

save(User user) 操作的 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内置了异常翻译机制,让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天全站免登陆