java HQL 与 Null 检查一对一关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7248278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HQL with Null check for one-to-one relation
提问by serg
I have the following one-to-one relation in Hibernate (that could be null):
我在 Hibernate 中有以下一对一关系(可能为空):
<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.
但这会返回所有实体,无论详细信息是否为空。
What would be a correct HQL then?
那么什么是正确的 HQL?
采纳答案by serg
Ok I found the solution:
好的,我找到了解决方案:
select e from Entity e join e.details d where d is not null
回答by tmarthal
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)
回答by bpgergo
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