这种hibernate的查询有问题吗!?

来源:百度知道 编辑:UC知道 时间:2024/05/17 17:41:39
我用的是strust2+hibernate+spring框架,请告诉我这种查询方法的写法对不对!!
谢谢!!
public List <Fsocialuser> fingFoAndProUserByUsername(String username){
String hql="from Fsocialuser fs where fs.provance=(select fs.provance from fs where fs.username=?) " + "and fs.fortes=(select fs.fortes from fs where fs.username=?)";
return getHibernateTemplate().find(hql,username);
}
上面有两处都需要username变量,在find里面我只用了一个usrename,这种写法对吗??
不对,请告诉我正确的写法!!谢谢了!!
首先谢谢了!!
我已经知道子查询返回的是单值啊!!
在很多书上都是有这种写法的啊!!
我也知道两个待定参数是需要两个实际传值,就是我不知道如果参数是相同的该怎么办!!
而且用两条以上的查询很浪费资源啊!!

HQL与你使用的Struts2 Spring没有任何关系
这样写是不正确的
Select fs.provance from fs where fs.username=?
select fs.fortes from fs where fs.username=?中间查询的结果返回的应该是一个集合,而你直接将返回值作为再检索条件显然不对,这样写难于操作,另外两个待定参数的场合,应该需要两个实际传值

建议解决案:
分拆这个HQL,先
select fs.provance, fs.fortes from fs where fs.username = ?
将取得的结果进行整理,得到唯一的fortes,provance
这样再进行
from Fsocialuser fs where fs.provance=? and fs.fortes=?
二次查询就行了

模型复杂,类层次很多,复杂的查询,建议使用sql,可参考下ibatis
take it easy