HQL with Null check for one-to-one relation

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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?

回答1:

Ok I found the solution:

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


回答2:

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 


回答3:

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!