jk_wangzw
hi_better
2016-04-22 06:30

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

  • hql
  • hibernate

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条回答

为你推荐

换一换