当前位置: 代码迷 >> J2EE >> 数据库多对多, fetche查询解决思路
  详细解决方案

数据库多对多, fetche查询解决思路

热度:30   发布时间:2016-04-21 21:46:00.0
数据库多对多, 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>();


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_,
  相关解决方案