使用 IN 子句更新 JPQL 中的用户实体字段
Update a user entity field in JPQL with IN clause
我有一个 class 用户,我需要更新 loginCount 和 lastLogin。
一些用户已经登录并更新了数据,所以我想在那里将 loginCount 更新为 1。
我尝试使用 JPQL。
我创建了一个查询,我正在发送 loginCount 变量 = 1, lastLogin作为 currentDate 和 ids 作为所有 id 的组合作为 CommaSeparatedString.
@Query("update User u set u.loginCount = :loginCount, u.lastLogin = :lastLogin where u.id IN (:ids)")
void updateUserInitialLoginCount(@Param("loginCount") Long loginCount, @Param("lastLogin") Date lastLogin, @Param("ids") String ids);
我想使用 'IN' 子句,但在这里我遇到了一个问题,即通过使用 IN 子句,我必须有一个逗号分隔的字符串,但我发现我的 ID 是 Long 类型。
ERROR:
Parameter value [1,2] did not match expected type [java.lang.Long(n/a)]
有办法解决吗?
您需要将 long 列表作为参数传递给 id 而不是字符串。
@Query("update User u set u.loginCount = :loginCount, u.lastLogin = :lastLogin where u.id IN (:ids)")
void updateUserInitialLoginCount(@Param("loginCount") Long loginCount, @Param("lastLogin") Date lastLogin, @Param("ids") List<Long> ids);
当您调用此方法时,创建一个 Long 数组列表并在其中添加 ID 并作为参数传递。
我有一个 class 用户,我需要更新 loginCount 和 lastLogin。 一些用户已经登录并更新了数据,所以我想在那里将 loginCount 更新为 1。
我尝试使用 JPQL。
我创建了一个查询,我正在发送 loginCount 变量 = 1, lastLogin作为 currentDate 和 ids 作为所有 id 的组合作为 CommaSeparatedString.
@Query("update User u set u.loginCount = :loginCount, u.lastLogin = :lastLogin where u.id IN (:ids)")
void updateUserInitialLoginCount(@Param("loginCount") Long loginCount, @Param("lastLogin") Date lastLogin, @Param("ids") String ids);
我想使用 'IN' 子句,但在这里我遇到了一个问题,即通过使用 IN 子句,我必须有一个逗号分隔的字符串,但我发现我的 ID 是 Long 类型。
ERROR: Parameter value [1,2] did not match expected type [java.lang.Long(n/a)]
有办法解决吗?
您需要将 long 列表作为参数传递给 id 而不是字符串。
@Query("update User u set u.loginCount = :loginCount, u.lastLogin = :lastLogin where u.id IN (:ids)")
void updateUserInitialLoginCount(@Param("loginCount") Long loginCount, @Param("lastLogin") Date lastLogin, @Param("ids") List<Long> ids);
当您调用此方法时,创建一个 Long 数组列表并在其中添加 ID 并作为参数传递。