簡體   English   中英

如何通過Spring API使用JPA查詢進行軟刪除?

[英]How to do soft delete using JPA query with spring api?

嗨,我是Java spring boot的初學者。 我試圖使用來修改特定記錄:

adminRepo.updateAllActive(id);    // method Call

倉庫:

@Query(value= "update admin a set a.active = 0 where a.id = :id " ,nativeQuery=true)
AdminEntity updateAllActive(@Param("id") Integer id);

但是我收到錯誤消息“無法提取結果集”

誰能為此指導我。 我能夠從數據庫中讀取條目。僅當我嘗試任何修改查詢時才會發生此問題。

提前致謝。

更新查詢返回一個整數值,指示受影響的條目數,並且可以覆蓋該整數值以返回void,因此,如果您不關心計數,則應將方法返回類型聲明為void:

@Modifying
@Query(value= "update admin a set a.active = 0 where a.id = :id" ,nativeQuery=true)
void updateAllActive(@Param("id") Integer id);

另外,您需要將查詢標記為@Modifying ,有關更新查詢的更多信息,請參閱文檔

添加@Modifying批注,並聲明您的方法以返回int

參見: https : //docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries

  @Modifying
    @Query("Update Users u SET u.userKey=:userKey, u.phoneNumberVerified=true, u.name=:name, u.surname=:surname, u.phoneNumberVerified=true WHERE u.phoneCode=:phoneCode AND u.phoneNumber=:phoneNumber ")
    void updateUsers(@Param("userKey") String userKey, @Param("phoneCode") String phoneCode,
            @Param("phoneNumber") String phoneNumber, @Param("name") String name, @Param("surname") String surname);

讓我知道是否正確

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM