<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 上的删除方法?

        How to override a delete method on a Spring Data CrudRepository?(如何覆盖 Spring Data CrudRepository 上的删除方法?)

      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 上的删除方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  • <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>

                        • <legend id='ZPd0x'><style id='ZPd0x'><dir id='ZPd0x'><q id='ZPd0x'></q></dir></style></legend>
                            <tbody id='ZPd0x'></tbody>