SQL supports several use cases for CASE clause, one of them:
Apparently, Criteria API doesn't handle correctly such use case. E.g. following code snippet
fails with exception:
Notable, code hibernate generates is actually correct and it could be executed to DB, but it fails with some kind of validation error.
It would be nice for Hibernate to handle such use cases of CASE clause.
Oracle JDK 1.8, Windows 10
The issue is that you can’t use predicates as expressions. Maybe Hibernate 6.0 will fix this. In the meantime, you can use the following:
Or the simpler version based on boolean logic