繁体   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