日期:2014-05-17  浏览次数:20727 次

关于ssh 中hibenate的一个问题
表A有id b_id 两个属性
表B有id value 两个属性
在entity A中定义
private Integer id ;
private B b; 
@ManyToOne(fetch = FetchType.LAZY
@JoinColumn(name = "b_id ")
public B getB() {
return B
}

A 中的b_id和B做了一个多对一的关联

在ADao中怎么实现对b_id 的查询?
我是下面这样写的,但是报错。。。
String="from A a where a.b_id = 1 "
public Query createQuery(String hql) {
Query query = getSession().createQuery(hql);
int j = values.length;
for (int i = 0; i < j; i++)
query.setParameter(i, values[i]);
return query;
}

我是想根据b_id 查询A表的数据
报错org.hibernate.QueryException: could not resolve property: b_id of: com.examples.entity.base.A [from com.examples.entity.base.A a where a.b_id = 1 ]

------解决方案--------------------
这么理解。hibernate是面向对象的。那个映射是对应数据里面的表和字段名。
HQL语句
from A a where a.b = ?
A 是 你写的那个实体类 对应数据库的表
a 就是A的一个实例
a.b 就是实例a的成员变量b。
------解决方案--------------------
from A a where a.b.id = 1