Hibernate JPA Case expression handling problem

In a JPA query with single item select list, if a case expression with entity result is used, the result of query is a list of empty arrays instead of a list of objects. for example if I have an entity named PersonalActor and have 10 records of data in database, the following query return a list that its size equals 10 and its content is empty array.

select case when pa.name = 'a1' then pa else pa end from PersonalActor pa     

the above query may seams somehow meaningless but note that it is just to show the problem.

test environment :

  • jdk 17

  • Hibernate 5.4.24.Final

  • JPA 2.1

to bring about the problem, a test case is created at GitHub - horakhsh/hibernate-test-case-templates: Templates and examples to report issues to Hibernate


This kind of query is illegal. You can only have scalar values in the result arms of case expressions, so if there is a bug, then it would be that we don’t throw an error during query parsing. I believe this is handled properly in Hibernate 6+ though, but the parser in Hibernate 5 is doing some weird stuff which might allow this kind of query to parse successfully, but produce rubbish results.