hibernate自关联的实现方式?
关于hibernate中自关联
一张具有父子关系的自关联表
id
name
pid
class A {
Integer id;
String name;
A parent;
}
使用hibernate装载一个A上来后,默认是会自动将parent也装载上来,
现在的问题是我装载
A
---B
------C
---------D
D上来 C B A 同时也成功装载上来了
但观察hibernat生成的sql只有两条
Hibernate:
select
producttyp0_.typeId as typeId3_1_,
producttyp0_.description as descript2_3_1_,
producttyp0_.name as name3_1_,
producttyp0_.parent_id as parent5_3_1_,
producttyp0_.visible as visible3_1_,
producttyp1_.typeId as typeId3_0_,
producttyp1_.description as descript2_3_0_,
producttyp1_.name as name3_0_,
producttyp1_.parent_id as parent5_3_0_,
producttyp1_.visible as visible3_0_
from
ProductType producttyp0_
left outer join
ProductType producttyp1_
on producttyp0_.parent_id=producttyp1_.typeId
where
producttyp0_.typeId=?
Hibernate:
select
producttyp0_.typeId as typeId3_1_,
producttyp0_.description as descript2_3_1_,
producttyp0_.name as name3_1_,
producttyp0_.parent_id as parent5_3_1_,
producttyp0_.visible as visible3_1_,
producttyp1_.typeId as typeId3_0_,
producttyp1_.description as descript2_3_0_,
producttyp1_.name as name3_0_,
producttyp1_.parent_id as parent5_3_0_,
producttyp1_.visible as visible3_0_
from
ProductType producttyp0_
left outer join
ProductType producttyp1_
on producttyp0_.parent_id=producttyp1_.typeId
where
producttyp0_.typeId=?
这是如何实现的?