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?
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.
JPQL can retrieve information or data using SELECT clause, can do bulk updates using UPDATE clause and DELETE clause.
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.
The Java Persistence query language (JPQL) is used to define searches against persistent entities independent of the mechanism used to store those entities.
Yes, it is possible with following:
select case when (count(scen) > 0) then true else false end from Scenario scen where scen.name = :name
What about just:
select count(scen) > 0 from Scenario scen where scen.name = :name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With