Customize order by in JPQL

By | 2013-10-07

Ref : http://stackoverflow.com/questions/9262413/hibernate-dynamic-order

For example, if there is a customize ordering in enum or a specific field.
We can use “Case when ? else ? end” syntax in order by.
(Supported in JPA)

SELECT o
FROM Cat o
ORDER BY CASE o.name WHEN ‘Tom’ THEN 0 ELSE 1 END

Leave a Reply

Your email address will not be published. Required fields are marked *