简体   繁体   English

HQL与Null检查一对一关系

[英]HQL with Null check for one-to-one relation

I have the following one-to-one relation in Hibernate (that could be null): 我在Hibernate中有以下一对一关系(可能为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: 我试图用HQL选择所有具有非空详细信息的实体:

from Entity e where e.details is not null

but this returns all entities, no matter whether details are null or not. 但是这会返回所有实体,无论细节是否为null。

What would be a correct HQL then? 那么什么是正确的HQL呢?

好的我找到了解决方案:

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

You can also most likely use the elements HQL function. 您也可能最有可能使用elements HQL函数。

From the Expressions section of HQL 3.3 Documentation HQL 3.3文档的表达式部分

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. 让我们假设这种一对一的关系是herpderp表映射的一部分,因此herpderp实体具有details属性。

Do you mean the query returns those herpderp records where the herpderp.details field is null? 你的意思是查询返回herpderp.details字段为空的那些herpderp记录?

Or do you mean something like this? 或者你的意思是这样的?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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