通过存储关联对象的id而不是关联对象本身来表示对象之间的关联是不常见的。存储对象更具表现力和类型安全,并且不需要对性能产生影响,因为 hibernate 具有延迟加载代理。
当然,您可以在映射文件中加入未映射为关联的事物。引用hibernate reference manual:
可以出现多个类,导致
在笛卡尔积或“交叉”中
加入。
from Formula, Parameter
from Formula as form, Parameter as param
and
查询可以返回多个对象
和/或属性作为类型数组
对象[]:
select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
或者作为一个列表:
select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
或者 - 假设类 Family
有一个适当的构造函数 - 作为
实际的类型安全 Java 对象:
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
您可以为选定的指定别名
使用 as 的表达式:
select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat
这在一起使用时最有用
选择新地图:
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat
此查询从别名返回 Map
到选定的值。
将其与一个简单的 where 子句结合,我们得到:
select product, item
from Product product, Item item
where item.product_id = product.product_id
编辑:我错过了你想要一个外部加入。在那种情况下,我不知道除了映射关联之外的其他方法,以便您可以对其进行普通连接。请注意,这不需要特定形式的查询结果,即您仍然可以这样做:
select product, item
from Item item
left join item.product as product