我有2个表,一个shop 还有一个shopcoupon 2者一对多关联关系,shop里面有2个数据,shopcoupon有且只有一条数据对应shop表,我用left join on 语句查询数据,按理说应该返回2条数据吧?,但是只返回了1条数据,而且好像还是2个对象类型的,我打印出来时2个实体对象类型的,是怎么回事啊?
相关代码:
shop.hbm.xml
-------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.joint.joint_ddw.entity.DdwShop" table="ddw_shop" catalog="joint_ddw">
        <id name="shopid" type="java.lang.Integer">
            <column name="shopid" />
            <generator class="native" />
        </id>
        <property name="shopname" type="java.lang.String">
            <column name="shopname" length="40" not-null="true" />
        </property>         。中间省略了一些        <property name="status" type="java.lang.String">
            <column name="status" length="1" not-null="true">
                <comment>N-新建, A-已审核, S-暂停, D-已删除</comment>
            </column>
        </property>
        <property name="mapaddress" type="java.lang.String">
            <column name="mapaddress" length="800" />
        </property>
        <set name="shopcoupons" outer-join="true" cascade="none">
         <key column="shopid" foreign-key="shopid"></key>
         <one-to-many class="com.joint.joint_ddw.entity.DdwShopcoupon"/>
        </set>
    </class>
</hibernate-mapping>
-----------------------------------
shopcoupon.hbm.xml
-----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.joint.joint_ddw.entity.DdwShopcoupon" table="ddw_shopcoupon" catalog="joint_ddw">
        <id name="couponid" type="java.lang.Integer">
            <column name="couponid" />
            <generator class="native" />
        </id>
        <property name="shopid" type="java.lang.Integer">
            <column name="shopid" not-null="true">
                <comment>0-禮品</comment>
            </column>
        </property>
        中间省略了一些
        <property name="discount" type="java.lang.String">
            <column name="discount" length="3" />
        </property>
    </class>
</hibernate-mapping>
----------------------------------------------------------
shop------------实体类
-------------------------------------------
package com.joint.joint_ddw.entity;import java.util.HashSet;
import java.util.Set;
public class DdwShop implements java.io.Serializable { // Fields private Integer shopid;
private String shopname;
private String phone;
private String address;
private String email;
private String website;
private String reArchitecture;
private String shopinfo;
private String hours;
private String recommend;
private Short chargeMod;
private String spending;
private Integer uid;
private String reason;
private String buslines;
private String ifPark;
private String ifBox;
private String ifTakeaway;
private String ifSupper;
private String ifWirelessnet;
private Integer areaid;
private Integer regionid;
private String shopimg;
private Integer shopgrade;
private String dishesid;
private String shoptypeid;
private Integer rate1;
private Integer rate2;
private Integer rate3;
private Integer rate4;
private Integer rate5;
private Integer rate6;
private Integer view;
private String status;
private String mapaddress;
private Set<DdwShopcoupon> shopcoupons=new HashSet<DdwShopcoupon>(); //省略get,set方法
}
-------------------------------------
shopcoupon-----实体类
-------------------------------------package com.joint.joint_ddw.entity;import java.util.Date;/**
 * DdwShopcoupon entity.
 * 
 * @author MyEclipse Persistence Tools
 */public class DdwShopcoupon implements java.io.Serializable { // Fields private Integer couponid;
private Integer shopid;
private Integer classid;
private Integer price;
private String title;
private String content;
private String couponimg;
private String logistics;
private Date postdate;
private Date expirydate;
private String discount;
         //省略get,set方法
}
-------------------------------------------
方法
------------------------------------
public List findShopSearch()throws Exception {

String sql="from DdwShop s "
+"left join s.shopcoupons sc where ";

return ddwShopDao.findShop(sql);
}
--------------------------------------------
调用
--------------------------------------------
                  List oldList=ddwShopService.findShopSearch();


for (int i = 0; i < oldList.size(); i++) {
System.out.println("-------------------");
Object[] oldObj=(Object[])oldList.get(i);
for (int j = 0; j < oldObj.length; j++) {
System.out.println(oldObj[j]);
}
}
-------------------------------
打印结果
-------------------------------
Hibernate: 
    select
        ddwshop0_.shopid as shopid16_0_,
        shopcoupon1_.couponid as couponid17_1_,
        ddwshop0_.shopname as shopname16_0_,
        ddwshop0_.phone as phone16_0_,
        ddwshop0_.address as address16_0_,
        ddwshop0_.email as email16_0_,
        ddwshop0_.website as website16_0_,
        ddwshop0_.re_architecture as re7_16_0_,
        ddwshop0_.shopinfo as shopinfo16_0_,
        ddwshop0_.hours as hours16_0_,
        ddwshop0_.recommend as recommend16_0_,
        ddwshop0_.charge_mod as charge11_16_0_,
        ddwshop0_.spending as spending16_0_,
        ddwshop0_.uid as uid16_0_,
        ddwshop0_.reason as reason16_0_,
        ddwshop0_.buslines as buslines16_0_,
        ddwshop0_.if_park as if16_16_0_,
        ddwshop0_.if_box as if17_16_0_,
        ddwshop0_.if_takeaway as if18_16_0_,
        ddwshop0_.if_supper as if19_16_0_,
        ddwshop0_.if_wirelessnet as if20_16_0_,
        ddwshop0_.areaid as areaid16_0_,
        ddwshop0_.regionid as regionid16_0_,
        ddwshop0_.shopimg as shopimg16_0_,
        ddwshop0_.shopgrade as shopgrade16_0_,
        ddwshop0_.dishesid as dishesid16_0_,
        ddwshop0_.shoptypeid as shoptypeid16_0_,
        ddwshop0_.rate1 as rate27_16_0_,
        ddwshop0_.rate2 as rate28_16_0_,
        ddwshop0_.rate3 as rate29_16_0_,
        ddwshop0_.rate4 as rate30_16_0_,
        ddwshop0_.rate5 as rate31_16_0_,
        ddwshop0_.rate6 as rate32_16_0_,
        ddwshop0_.view as view16_0_,
        ddwshop0_.status as status16_0_,
        ddwshop0_.mapaddress as mapaddress16_0_,
        shopcoupon1_.shopid as shopid17_1_,
        shopcoupon1_.classid as classid17_1_,
        shopcoupon1_.price as price17_1_,
        shopcoupon1_.title as title17_1_,
        shopcoupon1_.content as content17_1_,
        shopcoupon1_.couponimg as couponimg17_1_,
        shopcoupon1_.logistics as logistics17_1_,
        shopcoupon1_.postdate as postdate17_1_,
        shopcoupon1_.expirydate as expirydate17_1_,
        shopcoupon1_.discount as discount17_1_ 
    from
        joint_ddw.ddw_shop ddwshop0_ 
    left outer join
        joint_ddw.ddw_shopcoupon shopcoupon1_ 
            on ddwshop0_.shopid=shopcoupon1_.shopid 
-------------------
com.joint.joint_ddw.entity.DdwShop@1b258fa
com.joint.joint_ddw.entity.DdwShopcoupon@140d415

解决方案 »

  1.   

    sql  后面where多了的,呵呵,
      

  2.   

    from DdwShop s " 
    +"left join s.shopcoupons sc 红色部分改为left join fetch 应该就是你想要的结果
      

  3.   

    ,,,我想把shop表的2个数据全都查出来啊,但是只能查出来一条数据啊,
      

  4.   

    <set name="shopcoupons" outer-join="true" cascade="none" lazy="false">  
            <key column="shopid" foreign-key="shopid"> </key> 
            <one-to-many class="com.joint.joint_ddw.entity.DdwShopcoupon"/> 
    </set> 
      

  5.   

    ------------------- 
    com.joint.joint_ddw.entity.DdwShop@1b258fa 
    com.joint.joint_ddw.entity.DdwShopcoupon@140d415
    这是一条数据
      

  6.   

    我改了下HQL语句:select s.shopid,s.shopname,s.address,s.ifPark,s.ifBox," +
         "s.ifTakeaway,s.ifSupper,s.ifWirelessnet,s.shopimg,s.shopgrade," +
    "s.dishesid,sc.discount from DdwShop s "+
    "left join s.shopcoupons sc"
    这样打赢出来就明显了,只有一条数据,Lazy我也改false了,
    结果:
    -------------------
    1:0
    齐齐火锅(临江门店):1
    重庆市渝中区临江路邹容广场B栋2楼:2
    是:3
    是:4
    是:5
    是:6
    是:7
    :8
    55:9
    22:10
    8.8:11