Case When Then from SQL to HQL Hibernate

Viewed 101

I am trying to convert this code from SQL to HQL hibernate. Anyone can give a hand?

CASE WHEN s.id IS NULL THEN s.direction else l.direction end as street

1 Answers

Look at the documentation:

15.33. Searched CASE expressions

The searched form has the following syntax:

CASE [ WHEN {test_conditional} THEN {match_result} ]* ELSE {miss_result} END

List<String> nickNames = entityManager.createQuery(
    "select " +
    "   case " +
    "   when p.nickName is null " +
    "   then " +
    "       case " +
    "       when p.name is null " +
    "       then '<no nick name>' " +
    "       else p.name " +
    "       end" +
    "   else p.nickName " +
    "   end " +
    "from Person p", String.class )
.getResultList();

Hibernate provides exactly the same syntax for this expression.

Related