hibernate中的left join和left join fetch的问题

mdot 2010-04-18 09:43:16
我的Question和Answer是一对多的关系,我想做一个查询,比如
允许输入问题的标题,问题提问者,答案的解答时间,答案的解答者,就可以进行关联查询,比如输出如下:

问题标题 问题提问者 答案的解答时间,答案的解答者

由于一个问题有多个答案和解答者,而有的问题可能暂时没有解答者,因此采用的是left join连接,
并且如果用户输入答案的解答时间的话,则只需要列出对于这个解答时间的‘一方“的问题就可以了,


比如问题A有5个解答,解答时间都在2010-4-17日,则只需要列出

问题标题 问题提问者
A B
我现在是这样写的:
String hql = " select q from Question as q left join fetch q.Answer a where 1=1";
String c_hql = "select count(q) from Question q left join fetch q.Answer a where 1=1";

if(!"".equals(问题标题)&& null!=问题标题)
{
hql=hql+" and q.问题标题= '"+问题标题+"' ";
c_hql=c_hql+" and q.问题标题 = '"+问题标题+"' ";
}

if((!"".equals(beginTime)&&null!=beginTime)&&(!"".equals(endTime)&&null!=endTime))
{

hql=hql+" and a.createdate >= to_date('"+beginTime+"','yyyy-MM-dd HH24:mi:ss') and a.createdate<=to_date('"+endTime+"','yyyy-MM-dd HH24:mi:ss')";
c_hql=c_hql+" and a.createdate >= to_date('"+beginTime+"','yyyy-MM-dd HH24:mi:ss') and a.createdate<=to_date('"+endTime+"','yyyy-MM-dd HH24:mi:ss')";
}

但出现了错误,说:
query specified join fetching, but the owner of the fetched association was not present in the select list

为什么呢?要如何搞呀
...全文
493 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bunrise 2010-04-20
  • 打赏
  • 举报
回复
c_hql你取出的最后结果是一个结果集的总和
而在后面你用了fetch关键字,你要马上抓取Answer的信息,但是answer找不到他的所在类即Question,
所以会出现错误

你的c_hql吧fetch去掉
gdfloyd 2010-04-20
  • 打赏
  • 举报
回复
c_hql 就不用fetch了

67,512

社区成员

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

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