1. <i id='s9X7p'><tr id='s9X7p'><dt id='s9X7p'><q id='s9X7p'><span id='s9X7p'><b id='s9X7p'><form id='s9X7p'><ins id='s9X7p'></ins><ul id='s9X7p'></ul><sub id='s9X7p'></sub></form><legend id='s9X7p'></legend><bdo id='s9X7p'><pre id='s9X7p'><center id='s9X7p'></center></pre></bdo></b><th id='s9X7p'></th></span></q></dt></tr></i><div id='s9X7p'><tfoot id='s9X7p'></tfoot><dl id='s9X7p'><fieldset id='s9X7p'></fieldset></dl></div>
    <tfoot id='s9X7p'></tfoot>

    <small id='s9X7p'></small><noframes id='s9X7p'>

  2. <legend id='s9X7p'><style id='s9X7p'><dir id='s9X7p'><q id='s9X7p'></q></dir></style></legend>

      <bdo id='s9X7p'></bdo><ul id='s9X7p'></ul>

    1. Spring CrudRepository 异常

      时间:2024-08-24

        <small id='xizvl'></small><noframes id='xizvl'>

      • <tfoot id='xizvl'></tfoot>
          <tbody id='xizvl'></tbody>
          <bdo id='xizvl'></bdo><ul id='xizvl'></ul>

            <legend id='xizvl'><style id='xizvl'><dir id='xizvl'><q id='xizvl'></q></dir></style></legend>

                <i id='xizvl'><tr id='xizvl'><dt id='xizvl'><q id='xizvl'><span id='xizvl'><b id='xizvl'><form id='xizvl'><ins id='xizvl'></ins><ul id='xizvl'></ul><sub id='xizvl'></sub></form><legend id='xizvl'></legend><bdo id='xizvl'><pre id='xizvl'><center id='xizvl'></center></pre></bdo></b><th id='xizvl'></th></span></q></dt></tr></i><div id='xizvl'><tfoot id='xizvl'></tfoot><dl id='xizvl'><fieldset id='xizvl'></fieldset></dl></div>
                本文介绍了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 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:具有多个 in 运算符的 crudrepository findBy 方法签名? 下一篇:如何在不使用查询缓存的情况下缓存 Spring Data JPA 查询方法的结果?

                相关文章

              1. <i id='JVUSi'><tr id='JVUSi'><dt id='JVUSi'><q id='JVUSi'><span id='JVUSi'><b id='JVUSi'><form id='JVUSi'><ins id='JVUSi'></ins><ul id='JVUSi'></ul><sub id='JVUSi'></sub></form><legend id='JVUSi'></legend><bdo id='JVUSi'><pre id='JVUSi'><center id='JVUSi'></center></pre></bdo></b><th id='JVUSi'></th></span></q></dt></tr></i><div id='JVUSi'><tfoot id='JVUSi'></tfoot><dl id='JVUSi'><fieldset id='JVUSi'></fieldset></dl></div>
                <legend id='JVUSi'><style id='JVUSi'><dir id='JVUSi'><q id='JVUSi'></q></dir></style></legend>

                  <small id='JVUSi'></small><noframes id='JVUSi'>

                    • <bdo id='JVUSi'></bdo><ul id='JVUSi'></ul>

                    <tfoot id='JVUSi'></tfoot>