数据库多对多, fetche查询

用大炮轰开幸福的大门 2013-11-04 08:46:32
我的订单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>();


Junit Test:我在取订单信息时,也会把所关联的商品信息取出来,

String hql = "from Orders where id=1";
Query query = (Query) s.createQuery(hql);

List<Orders> list = query.list();

System.out.println(list.size());

下面是后台发的sql语句,
----------------------------------

 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=?



我的问题,我可以取到Orders 的集合,但是怎么样取到内存中的Goods 的集合呢?求解啊,这几天,写东西,要晕了,
...全文
190 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiuqiupeng 2013-11-05
  • 打赏
  • 举报
回复
你别把它看成是数据库,你直接操作对象就好了,orm的好处就是你可以不用关心数据库怎么存储的,你只需要关心你取出来的数据怎样操作就好了。

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧