Mik*_*e Q 13 hibernate join nullable outer-join
我在ProductDfn类中有这样的hibernate映射
@ManyToOne( fetch = FetchType.LAZY, optional = true )
@JoinColumn( name = "productTypeFk", nullable = true )
public ProductType getProductType()
{
return productType;
}
Run Code Online (Sandbox Code Playgroud)
请注意,该关系被定义为可选(并且该列可以为空).
做HQL的时候是这样的
select p.name as col1, p.productType.name as col2 from ProductDfn p
Run Code Online (Sandbox Code Playgroud)
内部联接用于将ProductDfn连接到ProductType,因为hibernate从select子句中的隐式连接生成显式SQL连接.
但是,当执行上述操作时,如果productType为null(在DB中),则由于内部联接而不返回任何行.对于这种关系,我希望hibernate默认为执行外连接(因为关系是可选的)所以我会得到col2的"null",而不是没有行.
有谁知道这是否可能?
谢谢.
Chs*_*y76 10
使用内部联接是因为您已p.productType.name在select子句中明确列出.如果ProductDfn您的提取设置为,则只需选择就不会发生这种情况LAZY.
如果您只需要检索这两个属性,则必须在查询中显式指定外部联接:
select p.name as col1, ptype.name as col2
from ProductDfn p
left join fetch p.productType ptype
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15830 次 |
| 最近记录: |