Update a user entity field in JPQL with IN clause

Gupta

New Member
#1
I have a class user where I need to update the loginCount and lastLogin. Some users have already logged in and updated data, so I want to update there loginCount to 1.

I tried to use JPQL.

I created a query, where I am sending loginCount variable = 1, lastLogin as currentDate and idsas a combination of all ids as CommaSeparatedString.
Mã:
@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);
I want to use 'IN' clause but here I got a problem that by to use IN clause I have to have a comma-delimited-string but my id which I am finding is of type Long.
ERROR:
Parameter value [1,2] did not match expected type [java.lang.Long(n/a)]
Is there a way to resolve this?
 

Admin

Administrator
Thành viên BQT
#2
You need to pass list of long as Param for ids instead of String.
Mã:
@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);
And when you call this method create an array list of Long and add the ids in it and pass as parameter.
 
Top