hibernate hql语句

hibernate  hql语句

涉及到的三个实体类和属性:

表一:TagGoods

private Long id;
private Tag tag_id;
private Goods goods_id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="tag_id")
public Tag getTag_id() {
return tag_id;
}
public void setTag_id(Tag tag_id) {
this.tag_id = tag_id;
}
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="goods_id")
public Goods getGoods_id() {
return goods_id;
}
public void setGoods_id(Goods goods_id) {
this.goods_id = goods_id;
}

表二:Tag

private Long id;

表三:Goods


查询数据库表TagGoods中tag_id = 1的集合。


session.createQuery("from TagGoods d where d.tag_id = '1'").list(); //d.tag_id = '1'

session.createQuery("from TagGoods d where d.tag_id =  1 ").list(); //d.tag_id = 1

//以上两种d.tag_id的值的类型不同,但是都能查询出来

session.createQuery("from TagGoods where tag_id.id = :tag_id ").setParameter("tag_id", 1).setMaxResults(num).list();

//使用setParameter("tag_id", 1)这种写法,则要写from TagGoods where tag_id.id = :tag_id


你可能感兴趣的:(Hibernate)