不应该忽略HQL可空属性,有帮助吗?

时间:2010-02-24 23:35:29

标签: join hql nullable

我有:

Class Foo
{
String name;
String value;
Foo parent; //Foo.parent is OneToOne and nullable
}

我有以下HQL:

FROM Foo f WHERE 
(lower(f.name) like concat( lower('string') , '%' )) or
(lower(f.value) like concat( lower('string') , '%' )) or
(lower(f.parent.name) like concat( lower('string') , '%' ))

查询效果很好,直到f.parent为null。即使f.name或f.value匹配,当f.parent为null时,结果也会被抛出。

所以说我有:

Foo a = new Foo();
a.name = "bob";
a.value = "chris";
a.parent = null;

Foo b = new Foo();
b.name = "Bob";
b.value="Chris";
b.parent = a;

当我搜索“b”时,只返回b。我想它,所以a和b被退回。

任何提示?

谢谢!

1 个答案:

答案 0 :(得分:1)

在查询中引用f.parent.name会在父属性上创建隐式inner join。要包含由于没有父级而无法连接的行,您必须明确使用left outer join代替:

from Foo f
left outer join f.parent as p
where
(lower(f.name) like concat( lower('string') , '%' )) or
(lower(f.value) like concat( lower('string') , '%' )) or
(lower(p.name) like concat( lower('string') , '%' ))

Related HQL documentation