JPQL statement returning boolean value

老子叫甜甜 提交于 2019-11-29 04:42:21

问题


Is it possible to write JPQL query like following:

select count(*) > 0 from Scenario scen where scen.name = :name

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

I would like to use the query this way:

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

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?


回答1:


Yes, it is possible with following:

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



回答2:


What about just:

select count(scen) > 0
from Scenario scen where scen.name = :name



回答3:


I was having the same problem, then I updated my hibernate to 4.3.11.Final and now it's working.



来源:https://stackoverflow.com/questions/12051563/jpql-statement-returning-boolean-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!