简体   繁体   中英

Spring Data JPA - SQL Grammar exception when trying to use IN clause with UPDATE Query

I am trying to update the entities in a certain list using Spring Data JPA. However, I'm getting SQL Grammar Exception.

This is the method:

@Modifying
@Query("UPDATE Call c set c.locationLocked = false, c.locationLockedBy = null, c.locationLockedOn = null WHERE c.callIdentifier IN :timedOutLockedCallsIdentifiers AND c.audit.retired = false")
int expireTimedOutLockedCalls(@Param("timedOutLockedCallsIdentifiers") List<String> timedOutLockedCallsIdentifiers);

And this is the root cause:

Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"

It would probably be easier to do it more programatically then in a query

In your repository have the following

@Query("SELECT c FROM Call c WHERE c.callIdentifier IN :timedOutLockedCallsIdentifiers AND c.audit.retired = :retired")
List<Call> findByCallIdentifiers(List<String> timedOutLockedCallsIdentifiers, Boolean retired)

Then in your class

List<Call> updated = callRepo.findByCallIdentifiers(identifiers, false)
for(Call c : updated) {
   c.setLocationLocked(false);
   c.setLocationLockedBy(null);
   c.setLocationLockedOn(null); 
}

callRepo.saveAll(updated);

This will call the proper underlying updates as needed and cant let spring handle the query syntax properly

You are passing null or empty list in IN clause of the query.

Please check timedOutLockedCallsIdentifiers before calling above query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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