数据库多对多, fetche查询
本帖最后由 erbao_vip 于 2013-11-04 08:47:49 编辑 我的订单Orders与Goods的映射关系,多对多的,设置了fetch 为eager,
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name = "order_goods", joinColumns = { @JoinColumn(name = "order_id") }, inverseJoinColumns = { @JoinColumn(name = "goods_id") })
private Set<Goods> goods = new HashSet<Goods>();
String hql = "from Orders where id=1";
Query query = (Query) s.createQuery(hql);
List<Orders> list = query.list();
System.out.println(list.size());
select
orders0_.orderId as orderId4_,
orders0_.BuyersMes as BuyersMes4_,
orders0_.addressid as addressid4_,
orders0_.buyNumbers as buyNumbers4_,
orders0_.goodsDecribe as goodsDec4_4_,
orders0_.goodsName as goodsName4_,
orders0_.goodsNumber as goodsNum6_4_,
orders0_.onePrice as onePrice4_,
orders0_.orderDateTime as orderDat8_4_,
orders0_.orderNumber as orderNum9_4_,
orders0_.orderStatus as orderSt10_4_,
orders0_.totalPrice as totalPrice4_,
orders0_.userid as userid4_
from
t_orders orders0_
where
orders0_.orderId=1
Hibernate:
select
orders0_.orderId as orderId4_,
orders0_.BuyersMes as BuyersMes4_,
orders0_.addressid as addressid4_,
orders0_.buyNumbers as buyNumbers4_,
orders0_.goodsDecribe as goodsDec4_4_,
orders0_.goodsName as goodsName4_,
orders0_.goodsNumber as goodsNum6_4_,
orders0_.onePrice as onePrice4_,
orders0_.orderDateTime as orderDat8_4_,
orders0_.orderNumber as orderNum9_4_,
orders0_.orderStatus as orderSt10_4_,
orders0_.totalPrice as totalPrice4_,
orders0_.userid as userid4_
from
t_orders orders0_
where
orders0_.orderId=1
08:34:41,854 DEBUG SQL:111 -
select
goods0_.order_id as order1_1_,
goods0_.goods_id as goods2_1_,
goods1_.goodsId as goodsId3_0_,
goods1_.carriageTime as carriage2_3_0_,
goods1_.collectCount as collectC3_3_0_,
goods1_.commodityStocks as commodit4_3_0_,
goods1_.goodsDecribe as goodsDec5_3_0_,
goods1_.goodsName as goodsName3_0_,
goods1_.goodsNumber as goodsNum7_3_0_,
goods1_.goodsType as goodsType3_0_,
goods1_.newPrice as newPrice3_0_,
goods1_.oldPrice as oldPrice3_0_,
goods1_.sales as sales3_0_
from
order_goods goods0_
left outer join
t_goods goods1_
on goods0_.goods_id=goods1_.goodsId
where
goods0_.order_id=?
Hibernate:
select
goods0_.order_id as order1_1_,
goods0_.goods_id as goods2_1_,
goods1_.goodsId as goodsId3_0_,
goods1_.carriageTime as carriage2_3_0_,
goods1_.collectCount as collectC3_3_0_,
goods1_.commodityStocks as commodit4_3_0_,
goods1_.goodsDecribe as goodsDec5_3_0_,
goods1_.goodsName as goodsName3_0_,
goods1_.goodsNumber as goodsNum7_3_0_,
goods1_.goodsType as goodsType3_0_,
goods1_.newPrice as newPrice3_0_,
goods1_.oldPrice as oldPrice3_0_,
goods1_.sales as sales3_0_
from
order_goods goods0_
left outer join
t_goods goods1_
on goods0_.goods_id=goods1_.goodsId
where
goods0_.order_id=?
for(int i=0;i<list.size();i++) {
Set<Goods> goods = list.get(i).goods;
}