Search code examples
javahibernatehql

conditional update using HQL


I'm trying to update some rows of my database using the following script which works pretty fine :

update DOS
set NAMEDOS=:name,
AGEDOS=:age,
WEIGHTDOS=:weight
where CODEDOS=:code

My problem is the following, sometimes the weight can be empty or null, so i have to just set the other properties.

I have tried to proceed like this, but it doesn't seems to work:

update DOS
set NAMEDOS=:name,
AGEDOS=:age,
WEIGHTDOS= (case when weight is not null then :weight else :WEIGHTDOS end),
where CODEDOS=:code

Can you help me please.


Solution

  • Try switching the brackets a bit and i think that :WEIGHTDOS should be without the double dots as its the direct column name not a parameter:

    update DOS
        set NAMEDOS=:name,
        AGEDOS=:age,
        WEIGHTDOS= case when (:weight is not null) then :weight 
                      else WEIGHTDOS 
                   end,
        where CODEDOS=:code