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
为什么呢?要如何搞呀