我有:
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被退回。
任何提示?
谢谢!
答案 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') , '%' ))