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

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

    <tfoot id='TjguQ'></tfoot>

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

        如何覆盖 Spring Data CrudRepository 上的删除方法?

        时间:2024-08-23

      2. <tfoot id='wWx07'></tfoot>
          <tbody id='wWx07'></tbody>
        <legend id='wWx07'><style id='wWx07'><dir id='wWx07'><q id='wWx07'></q></dir></style></legend>

          • <bdo id='wWx07'></bdo><ul id='wWx07'></ul>
                • <small id='wWx07'></small><noframes id='wWx07'>

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

                  本文介绍了如何覆盖 Spring Data CrudRepository 上的删除方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些无法删除的对象,必须更新一个名为已删除"的公共字段来代替它.我阅读了 那里使用 #{#entityName} 编写通用查询.出于这个原因,我尝试像这样覆盖 CrudRepository#delete(…) 方法:

                  I have some objects I cannot delete, and must update a common field named 'deleted' instead of it. I read there that I can write generic querys, using #{#entityName}. For that reason I tried to override CrudRepository#delete(…) method like this:

                  public interface DeleteableRepository<T, ID extends Serializable> extends CrudRepository<T,ID>{
                  
                      @Override
                      @Query("UPDATE #{#entityName} x set x.deleted = 1 where x.id = ?1")
                      public void delete(ID id);
                  }
                  

                  但是我有一个单元测试显示我错了!

                  But the I have a unit test that shows me wrong!

                  @Test
                  public void testDelete() {
                  
                      SomeDeleteableObject sdo = new SomeDeletableObject();
                      sdo = getDeleteableRepository().create(sdo);
                  
                      Assert.assertNotNull(sdo);
                      Assert.assertNotNull(sdo.getId());
                      Assert.assertFalse(sdo.isDeleted());
                      getDeleteableRepository().delete(sdo);
                  
                      sdo = getDeleteableRepository().findOne(sdo.getId());
                      //Fails here
                  
                  }
                  

                  难道不能像这样覆盖 CrudRepository 方法吗?

                  Isn't it possible to override CrudRepository methods like that?

                  推荐答案

                  要修改查询,您需要在方法中添加 @Modifying.

                  For modifying queries you need to add an @Modifying to the method.

                  确保您了解所选方法的副作用:

                  Be sure you are aware of the side effects of the approach you chose:

                  • 执行操纵查询几乎绕过了所有 EntityManager 缓存.因此,后续的 findOne(…) 可能/仍会返回您尝试删除的对象的旧实例,以防 EntityManager 已经加载它.为防止这种情况发生,请将 @Modifying 中的 clearAutomatically 标志设置为 true,但请注意,这将导致所有待处理的更改被清除.
                  • 对于基于查询的数据操作,no 生命周期回调将被触发,no 级联将在持久化上下文级别触发.这意味着,侦听 @PreUpdate 事件的实体侦听器将不会收到通知.还有任何级联操作
                  • Executing a manipulating query is pretty much bypassing all EntityManager caches. Thus a subsequent findOne(…) might/will still return the old instance of the object you tried to delete in case the EntityManager had already loaded it. To prevent that, set the clearAutomatically flag in @Modifying to true but be aware that this will cause all pending changes being wiped out.
                  • For query based data manipulation no lifecycle callbacks will be triggered and no cascades will be triggered on the level of the persistence context. This means, entity listeners listening to an @PreUpdate event will not get notified. Also any cascade operations

                  这篇关于如何覆盖 Spring Data CrudRepository 上的删除方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 JPQL 查询中过滤子集合? 下一篇:如何在没有 _class 属性的情况下将 spring 数据与 couchbase 一起使用

                  相关文章

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

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

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

                    1. <legend id='ZPd0x'><style id='ZPd0x'><dir id='ZPd0x'><q id='ZPd0x'></q></dir></style></legend>