JPA @Query with "case when exists" does not work with Hibernate

I have an @Repository interface with the following method of checking an IF database containing a collision of records (in the sense of a business domain) with one I am going to persist (I don't care that there are counter records):

@Query("select case when exists ( select me from MyEntity me where {my conditions regarding someParam here} ) then true else false end from MyEntity") boolean findColliding(@Param("someParam") String someParam); 

The table in which I run it is empty (PostgreSQL), so the subquery exists should not find anything, and I believe that the whole method should return false as the case state, it can return true if it exists, and false otherwise.

It returns null #facepalm

My query passes a query syntax check at startup (without QuerySyntaxException), but throws an exception on execution:

 org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract boolean findColliding(...) 

What am I doing wrong? Should I take a different approach to my problem?

Hibernate 5.0.11.Final

+5
source share
1 answer

JPA 2.1 CASE expression supports only scalar expressions , not queries.

See the JPA specification for more information.

If the database supports this syntax, you should use your own SQL query instead.

+6
source

Source: https://habr.com/ru/post/1260413/


All Articles