Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL statement returning boolean value

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?

like image 543
Piotr Sobczyk Avatar asked Aug 21 '12 09:08

Piotr Sobczyk


People also ask

How do you select a boolean in SQL?

SQL Server does not support a Boolean type e.g. SELECT WHEN CAST(1 AS BIT) THEN 'YES' END AS result -- results in an error i.e. CAST(1 AS BIT) is not the same logical TRUE.

Which of the following clauses are supported in JPQL?

JPQL can retrieve information or data using SELECT clause, can do bulk updates using UPDATE clause and DELETE clause.

Which is JPQL aggregate function?

JPQL supports the five aggregate functions of SQL: COUNT - returns a long value representing the number of elements. SUM - returns the sum of numeric values. AVG - returns the average of numeric values as a double value.

What do you understand by JPQL query?

The Java Persistence query language (JPQL) is used to define searches against persistent entities independent of the mechanism used to store those entities.


2 Answers

Yes, it is possible with following:

select case when (count(scen) > 0)  then true else false end   from Scenario scen where scen.name = :name 
like image 195
Mikko Maunu Avatar answered Sep 22 '22 06:09

Mikko Maunu


What about just:

select count(scen) > 0 from Scenario scen where scen.name = :name 
like image 28
Stefan Haberl Avatar answered Sep 23 '22 06:09

Stefan Haberl