I have the following one-to-one relation in Hibernate (that could be null):
<one-to-one name="details" class="com.example.Details" lazy="false" cascade="all"/>
I am trying to select all entities that have non-null details with HQL:
from Entity e where e.details is not null
but this returns all entities, no matter whether details are null or not.
What would be a correct HQL then?
The equivalent to the nvl command in HQL is the coalesce command. coalesce(a,b) will return a if a is not null, otherwise b .
Subqueries. For databases that support subselects, Hibernate supports subqueries within queries. A subquery must be surrounded by parentheses (often by an SQL aggregate function call). Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed.
HQL Join : HQL supports inner join, left outer join, right outer join and full join.
Save this answer. Show activity on this post. I didn't find any specification of the behavior in the Hibernate docs, but the between operator in HQL is translated to the between operator in SQL, which is inclusive.
Ok I found the solution:
select e from Entity e join e.details d where d is not null
You can also most likely use the elements
HQL function.
From the Expressions section of HQL 3.3 Documentation
from Cat cat where exists elements(cat.kittens)
Which should translate to your query as
Select Entity e where exists elements(e.details)
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