'case when exists' 的 JPA @Query 不适用于 Hibernate

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

我有一个 @Repository 接口,使用以下方法检查 IF 数据库是否包含与该记录冲突的记录(在业务领域的含义) 我要坚持(我不关心冲突记录是什么):

@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);

table 我 运行 它是空的 (PostgreSQL) 因此 exists 子查询应该找不到任何东西,我相信整个方法should return false as case 声明它只能 return 如果存在则为真,否则为假。

它return无效#facepalm

我的查询在启动时通过了查询语法检查(没有 QuerySyntaxException)但在执行时抛出异常:

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

我做错了什么?我应该采取其他方法来解决我的问题吗?

休眠 5.0.11.Final

JPA 2.1 CASE 表达式only supports scalar expressions,不是查询。

有关详细信息,请查看 JPA specification

如果数据库支持此语法,则需要使用a native SQL query