HQL with zero validation for reciprocal relationship

I have the following one-to-one relationship in Hibernate (this may 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-zero data using HQL:

 from Entity e where e.details is not null 

but this returns all entities, regardless of whether the data is null or not.

What will be the correct HQL?

+6
source share
3 answers

Ok, I found a solution:

 select e from Entity e join e.details d where d is not null 
+5
source

Suppose this one-to-one relationship is part of the display of the herpderp table, so the herpderp object has the details property.

You mean the query returns these herpderp entries, where the herpderp.details field is null?

Or do you mean something like this?

 from Entity e where e.details.someDetailsField is not null 
+1
source

You can also use the HQL elements function.

In the "Expressions" HQL 3.3 Documentation

 from Cat cat where exists elements(cat.kittens) 

What should translate to your request as

 Select Entity e where exists elements(e.details) 
+1
source

Source: https://habr.com/ru/post/896238/


All Articles