2 jk wangzw jk_wangzw 于 2016.04.22 14:30 提问

Hibernate一对多中,在多方的关联对象如果提供getter方法,hql查询不出结果的问题

entity

 @Entity
@Table(name = "tb_many", catalog = "ecs")
public class Many implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id 
    @Column(name="mid")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer mid;
    private String mname;

    @ManyToOne(targetEntity=One.class)  
    @JoinColumn(name="oid")
    private One one;



    public Integer getMid() {
        return mid;
    }

    public void setMid(Integer mid) {
        this.mid = mid;
    }

    public String getMname() {
        return mname;
    }

    public void setMname(String mname) {
        this.mname = mname;
    }

    public One getOne() {
        return one;
    }

    public void setOne(One one) {
        this.one = one;
    }

    public Many() {
        // TODO Auto-generated constructor stub
    }

}

 @Entity
@Table(name = "tb_one", catalog = "ecs")
public class One implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id 
    @Column(name="oid")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer oid;
    private String oname;

    @OneToMany(targetEntity=Many.class, mappedBy="one", fetch=FetchType.EAGER)
    private Set<Many> manys;



    public Integer getOid() {
        return oid;
    }

    public void setOid(Integer oid) {
        this.oid = oid;
    }

    public String getOname() {
        return oname;
    }

    public void setOname(String oname) {
        this.oname = oname;
    }

    public Set<Many> getManys() {
        return manys;
    }

    public void setManys(Set<Many> manys) {
        this.manys = manys;
    }

    public One() {
        // TODO Auto-generated constructor stub
    }


}
 String hql = "from One";           
            Query query = session.createQuery(hql);
            list = query.list();

Hibernate sql

 Hibernate: select one0_.oid as oid1_3_, one0_.oname as oname2_3_ from ecs.tb_one one0_
Hibernate: select manys0_.oid as oid3_3_0_, manys0_.mid as mid1_1_0_, manys0_.mid as mid1_1_1_, manys0_.mname as mname2_1_1_, manys0_.oid as oid3_1_1_ from ecs.tb_many manys0_ where manys0_.oid=?
Hibernate: select manys0_.oid as oid3_3_0_, manys0_.mid as mid1_1_0_, manys0_.mid as mid1_1_1_, manys0_.mname as mname2_1_1_, manys0_.oid as oid3_1_1_ from ecs.tb_many manys0_ where manys0_.oid=?
Hibernate: select manys0_.oid as oid3_3_0_, manys0_.mid as mid1_1_0_, manys0_.mid as mid1_1_1_, manys0_.mname as mname2_1_1_, manys0_.oid as oid3_1_1_ from ecs.tb_many manys0_ where manys0_.oid=?

查不出任何结果
如果把Many中的

 public One getOne() {
        return one;
    }

注释掉,则可以正常查询
哪位高手知道我错在何处,好揪心。。

1个回答

devmiao
devmiao   Ds   Rxr 2016.04.22 14:34
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!