Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HQL with Null check for one-to-one relation

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?

like image 564
serg Avatar asked Aug 30 '11 18:08

serg


People also ask

What is NVL in HQL?

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 .

Is subquery supported in HQL?

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.

Can we use join in HQL query?

HQL Join : HQL supports inner join, left outer join, right outer join and full join.

Is HQL between inclusive?

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.


2 Answers

Ok I found the solution:

select e from Entity e join e.details d where d is not null
like image 108
serg Avatar answered Oct 06 '22 01:10

serg


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)
like image 22
tmarthal Avatar answered Oct 06 '22 02:10

tmarthal