返回布尔值的 JPQL 语句 [英] JPQL statement returning boolean value

查看:48
本文介绍了返回布尔值的 JPQL 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写如下的 JPQL 查询:

Is it possible to write JPQL query like following:

选择计数(*)>0 来自 Scenario scen where scen.name = :name

这将根据实体填充条件是否存在返回真/假布尔值?

that would return true/false boolean values depending of whether entity filling criteria exists or not?

我想这样使用查询:

boolean exists = entityManager.createQuery(query,Boolean.class).setParameter("name",name).getSingleResult();

我示例中的查询在语法上不正确(解析错误),但是否有任何正确的方法可以在 JPQL 中进行类似检查,返回布尔值,还是只能在 Java 代码中实现?

The query from my example just isn't syntactically correct (parse error), but is there any correct way of doing checks like that in JPQL, that would return boolean value, or is it only possible in Java code?

推荐答案

是的,可以通过以下方式实现:

Yes, it is possible with following:

select case when (count(scen) > 0)  then true else false end  
from Scenario scen where scen.name = :name

这篇关于返回布尔值的 JPQL 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆