Which JPA provider are you using?
Give it a try
SELECT u.userId,
(CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END) as myNum
FROM User u
ORDER BY u.userId, myNum DESC
or,
SELECT new com.systemname.to.UserDataHolder(u.userId,
CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END)
FROM User u
ORDER BY u.userId, CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END DESC
James source
share