简体   繁体   English

带有'case at exists'的JPA @Query不适用于Hibernate

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

I have a @Repository interface with the following method for checking IF database contains a record colliding (in business domain meaning) with the one I'm about to persist (I don't care about WHAT the colliding records are): 我有一个带有以下方法的@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);

The table on which I run it is empty (PostgreSQL) therefore the exists subquery shouldn't find anything and I believe the whole method should return false as case states it can only return true if exists and false otherwise. 关于这一点我运行它是空的(PostgreSQL的)表因此存在不子查询应该找到任何东西,我相信作为案例指出,如果存在,否则为false只能返回true整个方法应返回false。

It returns null #facepalm 它返回null #facepalm

My query passes query syntax check on startup (no QuerySyntaxException) but throws exception when executed: 我的查询在启动时传递查询语法检查(没有QuerySyntaxException)但在执行时抛出异常:

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 some other approach for my problem? 我应该采取其他方法解决我的问题吗?

Hibernate 5.0.11.Final Hibernate 5.0.11.Final

JPA 2.1 CASE expression only supports scalar expressions , not queries. JPA 2.1 CASE表达式仅支持标量表达式 ,而不支持查询。

For more details, check out the JPA specification . 有关更多详细信息,请查看JPA规范

If the DB supports this syntax, you need to use a native SQL query instead. 如果数据库支持此语法,则需要使用本机SQL查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM