Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA / JPQL - bulk update

Tags:

jpa

jpql

I have to perform a bulk update on a table. Making a fast example :

 UPDATE Book b SET b.amount = b.amount + 1 WHERE b IN ( :books )

The problem is that b.amount can be or a NULL value or an int, and if there is a NULL value it should behave as b.amount would be equal to 1.

Is there any "cast" in JPA/JPQL or any other way to work-around this problem,

Thank you in advance,

Regards, P

like image 685
redbull Avatar asked Feb 24 '23 22:02

redbull


1 Answers

You should be able to use COALESCE:

UPDATE Book b SET b.amount = COALESCE(b.amount, 1) + 1 WHERE b IN ( :books ) 
like image 171
axtavt Avatar answered Mar 27 '23 21:03

axtavt