Alex February 2016

How do I use "order by case when ..." in HQL?

I want to execute my HQL query like this:

Query queryPayment=sixSession.createQuery("SELECT first_name, last_name FROM Table1 WHERE first_name =:f_name or last_name=:l_name ORDER BY
   CASE WHEN first_name =:f_name THEN 0 ELSE 1 END)
        .setParameter("first_name", f_name)
        .setParameter("last_name", l_name);

but it does not work. am I missing something?

Answers


v.ladynev February 2016

Names of your parameters f_name and l_name. Change this

setParameter("first_name", f_name)
.setParameter("last_name", l_name)

to this

setParameter("f_name", f_name)
.setParameter("l_name", l_name)

You can't use named parameters in the ORDER BY. But you can just put it using String.format().

Post Status

Asked in February 2016
Viewed 1,797 times
Voted 5
Answered 1 times

Search




Leave an answer