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

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

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

        Spring Data Repository @Query - 更新并返回修改后的实体

        时间:2024-08-24

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

              <tbody id='6XxIh'></tbody>

            <small id='6XxIh'></small><noframes id='6XxIh'>

          1. <legend id='6XxIh'><style id='6XxIh'><dir id='6XxIh'><q id='6XxIh'></q></dir></style></legend>
          2. <tfoot id='6XxIh'></tfoot>
                1. 本文介绍了Spring Data Repository @Query - 更新并返回修改后的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我们有一个带有自定义方法的 Spring Data 存储库接口...

                  let's assume we have a Spring Data repository interface with a custom method...

                  @Modifying
                  @Transactional
                  @Query("UPDATE MyEntity SET deletedAt = CURRENT_TIMESTAMP WHERE id = ?1")
                  void markAsSoftDeleted(long id);
                  

                  这个方法只是简单的设置实体的deletedAt字段,ok.有什么方法可以让这个方法返回 MyEntity 的更新版本?

                  This method simply sets the deletedAt field of the entity, ok. Is there any way to allow this method to return an updated version of the MyEntity?

                  显然……

                  @Modifying
                  @Transactional
                  @Query("UPDATE MyEntity SET deletedAt = CURRENT_TIMESTAMP WHERE id = ?1")
                  MyEntity markAsSoftDeleted(long id);
                  

                  ...不起作用,因为...

                  ...does not work, since...

                  java.lang.IllegalArgumentException:修改查询只能使用void或int/Integer作为返回类型!

                  java.lang.IllegalArgumentException: Modifying queries can only use void or int/Integer as return type!

                  是否有人知道另一种方法可以轻松实现这一点,当然除了明显的在存储库和调用者之间为此类事情添加服务层"...

                  Does anyon know another way to easily allow that, except of course the obvious "add a service layer between repository and caller for such things"...

                  推荐答案

                  在@Modifying annotation 上设置 clearAutomatically 属性.这将清除 EntityManager 中所有未刷新的值.

                  Set clearAutomatically attribute on @Modifying annotation.That will clear all the non-flushed values from EntityManager.

                  @Modifying(clearAutomatically=true)
                  @Transactional
                  @Query("UPDATE MyEntity SET deletedAt = CURRENT_TIMESTAMP WHERE id = ?1")
                  MyEntity markAsSoftDeleted(long id);
                  

                  要在提交更新之前刷新您的更改,最新的 spring-data-jpa 在 @ModifyingAttribute 上有另一个属性.但我认为它仍在 2.1.M1 版本中.

                  To flush your changes before committing the update latest spring-data-jpa has another attribute on @ModifyingAttribute. But I think its still in 2.1.M1 release.

                  @Modifying(clearAutomatically=true, flushAutomatically = true)
                  

                  请查看对应的jira bug request:https://jira.spring.io/browse/DATAJPA-806

                  Please check corresponding jira bug request: https://jira.spring.io/browse/DATAJPA-806

                  另一种方法是您可以实现自定义存储库实现并在完成查询执行后返回您更新的实体.

                  Another approach can be you can implement custom repostiory Implementation and return your updated entity after done with the query execution.

                  参考:Spring 数据 jpa自定义存储库实现

                  这篇关于Spring Data Repository @Query - 更新并返回修改后的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Spring Data REST 中公开 @EmbeddedId 转换器 下一篇:如何在 Spring Boot 中注册自定义转换器?

                  相关文章

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

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

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

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