比如有两个实体,Person和Food
Person有Long id,String sex和Set<Food> foods三个属性
Food有Long id,Date date和Person属性,其中的date记录吃食物的时间
现在我要查询性别为男的(sex为男)Person的一个List,要求Food的时间为本月
我的hql为select e from Person e left outer join e.foods p with p.planDate between ? and ? where e.sex=?
这个语句查出来的是food在当月的Person,而我的需求是查出来所有的Person,既即使这个Person本月没有food也要列出来请问应该如何实现
Person有Long id,String sex和Set<Food> foods三个属性
Food有Long id,Date date和Person属性,其中的date记录吃食物的时间
现在我要查询性别为男的(sex为男)Person的一个List,要求Food的时间为本月
我的hql为select e from Person e left outer join e.foods p with p.planDate between ? and ? where e.sex=?
这个语句查出来的是food在当月的Person,而我的需求是查出来所有的Person,既即使这个Person本月没有food也要列出来请问应该如何实现
解决方案 »
- jsp初学者 求教
- tomcat6.0 在myeclipse 7.0启动时会报这样的错
- cannot be resolved to a type
- 在线求救!!异常:Cannot find bean: "org.apache.struts.taglib.html.BEAN" in any scope
- 急!B/S的袖珍库存管理系统
- 关于数据库连结池的问题?
- 2004,祝csdn所有朋友新年快乐~~~~~
- 怎样在网页链接中直接打开 .doc文件?在JSP 里是不是还有什么限制呢?
- 请问jsp调用javabean为什么会报错包找不到呢?急死了
- 请问如何改变request变量的值?急,在线等,请救命!
- JSP传递的参数中有特殊字符,servlet中接收不完全
- url传参数
也就是说,with后面的过滤条件是在连接查询之后才过滤,我需要的是先对Food进行过滤然后和Person连接
请问应该怎么办到
我这么写了之后就会报错unexpected token,是括号里的子查询不能识别,那么正确的写法应该是什么样的呢
hql我没有写过几个,不过 你这里 left outer join (from Food etp where etp.date between ? and ?) 好像没有给出连接条件。
from Person e left outer join Food etp with etp.date between ? and ? where e.sex=?
你就不可以查询出来结果之后,在将food的person过滤掉不就可以了吗?
比如
StringBuffer sql=new StringBuffer("select * from Person as p,Food as f where p.id=fid and 1=1 ");
if(date!=null){
sql.buffer(" and f.date between xxx and xxx ");
}
if(sex!=null){
sql.buffer(" and p.sex=xxx")
}session.createSQL(sql).addEntity(Person.class);
比如一共有10个person,其中一个person有两个food,那么查出的list<person>居然会有十一条。。
也就是说有两条一模一样的person的被查出来,这应该怎么解决啊