【问题标题】:Update a user entity field in JPQL with IN clause使用 IN 子句更新 JPQL 中的用户实体字段
【发布时间】:2018-11-15 15:14:26
【问题描述】:

我有一个班级用户,我需要更新 loginCountlastLogin。 有些用户已经登录并更新了数据,所以我想将那里的 loginCount 更新为 1。

我尝试使用 JPQL。

我创建了一个查询,我将 loginCount 变量 = 1lastLogin 作为 currentDate 和 ids 作为 CommaSeparatedString 的所有 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") String ids);

我想使用“IN”子句,但在这里我遇到了一个问题,即通过使用 IN 子句,我必须有一个逗号分隔的字符串,但我发现的 id 是 Long 类型。

ERROR:
Parameter value [1,2] did not match expected type [java.lang.Long(n/a)]

有没有办法解决这个问题?

【问题讨论】:

  • 您需要将ids 作为List<Long> ids 传递,并在方法参数中更改ids 的参数类型。
  • 感谢 Erfan Ahmed Emon

标签: java hibernate jpql


【解决方案1】:

您需要为 ids 而不是 String 传递 long 作为 Param 的列表。

@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 并作为参数传递。

【讨论】:

  • 谢谢,Pooja,我早该知道的。 TCH
猜你喜欢
  • 2019-03-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 2021-02-13
相关资源
最近更新 更多