简体   繁体   中英

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?

好的我找到了解决方案:

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)

Let's suppose this one-to-one relation is part of the mapping of herpderp table, hence herpderp entity has the details property.

Do you mean the query returns those herpderp records where the herpderp.details field is null?

Or do you mean something like this?

from Entity e where e.details.someDetailsField is not null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM