急问:hibernate 多对一查询问题

e_first 2007-03-06 12:14:54
表product、company是多对一关系,product中有com_id字段,我想根据com_id查询出所有product信息(使用对象查询方式),Product.hbm.xml中的<many-to-one>元素lazy="false" outer-join="true"。
ProductBean product = new ProductBean();
CompanyBean company = new CompanyBean();
company.setId("001");
product.setCompany(company);
然后调用ProductHibDAO的findByExample(Product instance)方法
结果把所有product信息都查询出来了
请问这是怎么回事
...全文
413 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
e_first 2007-03-06
  • 打赏
  • 举报
回复
ProductHibDAO中的方法如下:
public List findByExample(Product instance) {
log.debug("finding Product instance by example");
try {
List results = new ArrayList();
// 按条件默认查询
Criteria criteria = getSession().createCriteria( "com.daqin.gridouter.data.vo.Product").add(Example.create(instance));
results = criteria.list();

return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

ProductBO中的方法如下:
public List getProductDefault(){

ProductBean product = new ProductBean();
CompanyBean company = new CompanyBean();
company.setId("001");
product.setCompany(company);

List defaultList = this.getProductDAO().findByExample(product);
return defaultList;
}
kafka0102 2007-03-06
  • 打赏
  • 举报
回复
把代码都贴上来吧。如果要查询product列表,sql语句应该类似于select * from Product where com_id=?,如果在ProductHibDAO实现,方法应该类似于findList(CompanyBean com)怎么会是findByExample(Product instance)呢?至于hql,where后的参数换成<many-to-one>中的配置就行了,这样的问题可以看看hibernate文档。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧