简体   繁体   English

如何通过Spring API使用JPA查询进行软删除?

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

Hi I am a beginner in Java spring boot. 嗨,我是Java spring boot的初学者。 I was trying to modify a particular record using : 我试图使用来修改特定记录:

adminRepo.updateAllActive(id);    // method Call

Repository: 仓库:

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

But I am getting error "Could not extract Resultset" 但是我收到错误消息“无法提取结果集”

Can anyone guide me for this. 谁能为此指导我。 I am able to read entries from database.Issue only occurs when i try any modification query. 我能够从数据库中读取条目。仅当我尝试任何修改查询时才会发生此问题。

Thanks in advance. 提前致谢。

Update query returns an integer value indicating the number of affected entries and can be overwritten to return void, so you should declare your method return type as void if you don't care about the count: 更新查询返回一个整数值,指示受影响的条目数,并且可以覆盖该整数值以返回void,因此,如果您不关心计数,则应将方法返回类型声明为void:

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

Also you need to mark your query as @Modifying , for more info regarding update queries refer to documentation . 另外,您需要将查询标记为@Modifying ,有关更新查询的更多信息,请参阅文档

Add @Modifying annotation, and declare your method to return int . 添加@Modifying批注,并声明您的方法以返回int

See: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries 参见: https : //docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries

Example

  @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);

Let me know if it correct 让我知道是否正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM