本文是想通过例子,证明hibernate双向多对多的情况下,为什么相互依赖了,还没有死循环。(可以下载例子 http://pan.baidu.com/s/1skjYSZb)
customers : orders = n : n,中间的关系表是 customers_orders_link。
Customers 类:
private Long id;
private String userName;
private Set orders = new HashSet();
Orders 类:
private Long id;
private String orderDescription;
private Set customers = new HashSet();
Customers 表
Orders表
customers_orders_link表
测试函数:
Customers customers = (Customers) session.get(Customers.class, 7);
后台执行的sql:
1.查找custormer通过的id
select
customers0_.ID as ID0_0_,
customers0_.userName as userName0_0_
from
customers customers0_
where
customers0_.ID=7
select
orders0_.cid as cid1_,
orders0_.oid as oid1_,
orders1_.ID as ID2_0_,
orders1_.orderDescription as orderDes2_2_0_
from
customers_orders_link orders0_
left outer join
orders orders1_
on orders0_.oid=orders1_.ID
where
orders0_.cid=7
Hibernate:
select
customers0_.oid as oid1_,
customers0_.cid as cid1_,
customers1_.ID as ID0_0_,
customers1_.userName as userName0_0_
from
customers_orders_link customers0_
left outer join
customers customers1_
on customers0_.cid=customers1_.ID
where
customers0_.oid=8
Hibernate:
select
customers0_.oid as oid1_,
customers0_.cid as cid1_,
customers1_.ID as ID0_0_,
customers1_.userName as userName0_0_
from
customers_orders_link customers0_
left outer join
customers customers1_
on customers0_.cid=customers1_.ID
where
customers0_.oid=9
Hibernate:
select
customers0_.oid as oid1_,
customers0_.cid as cid1_,
customers1_.ID as ID0_0_,
customers1_.userName as userName0_0_
from
customers_orders_link customers0_
left outer join
customers customers1_
on customers0_.cid=customers1_.ID
where
customers0_.oid=11
select
orders0_.cid as cid1_,
orders0_.oid as oid1_,
orders1_.ID as ID2_0_,
orders1_.orderDescription as orderDes2_2_0_
from
customers_orders_link orders0_
left outer join
orders orders1_
on orders0_.oid=orders1_.ID
where
orders0_.cid=10