遇到一个非常奇怪的问题:session.createQuery

青柠loft 2013-02-28 04:48:28

public List getPatientsListByUid(final VPlist vplist,final int pageNo,final int pageSize,final int ulevel) {
try {
if (vplist!=null && vplist.getUid()>0) {

List list = getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {

//String hql = "from TPatients where uid=? order by pstate,pctime";
String hql = null;
Query query =null;
if(ulevel==2){
hql = "from VPlist where uid=? order by pstate,pctime";
query = session.createQuery(hql);
query.setInteger(0, vplist.getUid());
}else if(ulevel==9){
hql = "from VPlist where hunitid=? and pstate = 2 order by pstate,pctime";
query = session.createQuery(hql);
query.setInteger(0, vplist.getHunitid());
}else if(ulevel==99){
hql = "from VPlist where pstate = 2 order by pstate,pctime";
query = session.createQuery(hql);
}
int first = pageSize*(pageNo-1);
query.setFirstResult(first);
query.setMaxResults(pageSize);
return query.list();
}

});
return list;
}else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}

}



public List getPatientsSearchByPnumber(final int uid,final int ulevel,final int sel_City,final int sel_Hunit,
final int state,final int name_pnumber,final String n_p_value,final int pageSize,final int pageNo){
try{
List list = getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuffer sb = new StringBuffer("from VPlist where");
if(sel_City>=0){
sb.append(" areaId = "+sel_City+" and");
}if(sel_Hunit>=0){
sb.append(" hunitid = "+sel_Hunit+" and");
}if(state>=0 && ulevel==2){
sb.append(" pstate = "+state+" ");
}if(name_pnumber==1){
sb.append(" pnumber = '"+n_p_value+"' and");
}if(name_pnumber==2){
sb.append(" gsName = '"+n_p_value+"' and");
}if(ulevel==9 || ulevel==99){
sb.append(" pstate = 2 and");
}else{
sb.append(" uid = "+uid);
}
String hql = sb.toString();
System.out.println(hql);
Query query = session.createQuery(hql);
int first = pageSize*(pageNo-1);
query.setFirstResult(first);
query.setMaxResults(pageSize);
return query.list();
}
});
return list;
}catch(Exception e){
e.printStackTrace();
return null;
}

}



public int getPatientsSearchCount(int uid,int ulevel,int sel_City,int sel_Hunit,
int state,int name_pnumber,String n_p_value){
int count = 0;
List list = null;
try {
StringBuffer sb = new StringBuffer("from VPlist where 1=1 ");
if(sel_City>=0){
sb.append(" and areaId = "+sel_City+" ");
}if(sel_Hunit>=0){
sb.append(" and hunitid = "+sel_Hunit+" ");
}if(state>=0 && ulevel==2){
sb.append(" and pstate = "+state+" ");
}if(name_pnumber==1){
sb.append(" and pnumber = '"+n_p_value+"' ");
}if(name_pnumber==2){
sb.append(" and gsName = '"+n_p_value+"' ");
}if(ulevel==9 || ulevel==99){
sb.append(" and pstate = 2 ");
}else{
sb.append(" and uid = "+uid);
}
String hql = sb.toString();
list = getHibernateTemplate().find(hql);
if (list!=null && list.size()>0) {
count = list.size();
}
} catch (Exception e) {
e.printStackTrace();
return 0;
}
return count;
}


第一个段和第三个段代码查出的数据都是正确的,但是第二个确查不出数据,我就郁闷了~!啥情况啊~!求指点
...全文
11369 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
青柠loft 2013-03-01
  • 打赏
  • 举报
回复
问题解决了~!!谢谢大家~! 原来是这样的。。。我吧pageSize和pageNo的参数写反了~!!!!!艹,我怎么这么粗心~!
yangxxxxx 2013-03-01
  • 打赏
  • 举报
回复
既然你已经继承了HibernateDaoSupport,为什么还要用session.createQuery(hql)呢?
青柠loft 2013-03-01
  • 打赏
  • 举报
回复
现在的问题是,本身如果 HQL=from VPList where uid = 1;数据库中会有30多条数据,可是Query query = session.createQuery(hql);只能获得到一条,如果用list = getHibernateTemplate().find(hql);就能全部获取到
青柠loft 2013-03-01
  • 打赏
  • 举报
回复
引用 11 楼 weiyuexiaoxuan 的回复:
if(ulevel==9 || ulevel==99){ sb.append(" pstate = 2 and"); }else{ sb.append(" uid = "+uid); } 你这行语句写错了吧。。应该是下面这样吧。 if(uleve……
将getPatientsSearchByPnumber()中的HQL语句改成getPatientsSearchCount()中的HQL语句,也不行~!
weiyuexiaoxuan 2013-02-28
  • 打赏
  • 举报
回复
if(ulevel==9 || ulevel==99){ sb.append(" pstate = 2 and"); }else{ sb.append(" uid = "+uid); } 你这行语句写错了吧。。应该是下面这样吧。 if(ulevel==9 || ulevel==99){ sb.append(" pstate = 2 "); }else{ sb.append(" uid = "+uid); } 你要是加and,而if又刚好满足的话,不就语句没结束么。
青柠loft 2013-02-28
  • 打赏
  • 举报
回复
引用 9 楼 a_b_a_b_a_b_a_b 的回复:
后台运行报错吗?debug一下呢?
后台什么错误都没有~!debug的值是""
a_b_a_b_a_b_a_b 2013-02-28
  • 打赏
  • 举报
回复
后台运行报错吗?debug一下呢?
青柠loft 2013-02-28
  • 打赏
  • 举报
回复
引用 7 楼 fangmingshijie 的回复:
看你hql,打印list.size有啥用
list里面有值,HQL怎么可能错呢?是吧?
  • 打赏
  • 举报
回复
看你hql,打印list.size有啥用
青柠loft 2013-02-28
  • 打赏
  • 举报
回复
引用 5 楼 fangmingshijie 的回复:
.. 看看先
如果我在第二个代码的Query query = session.createQuery(hql);前面加上

List list = getHibernateTemplate().find(hql);
System.out.println(list.size());
这个List就能得到正确的值~!但是query 怎么就得不到呢??
  • 打赏
  • 举报
回复
.. 看看先
青柠loft 2013-02-28
  • 打赏
  • 举报
回复
引用 3 楼 fangmingshijie 的回复:
最简单的方法就是打印出来hql,看看是什么,问题就一目了然了
问题是打印出来的SQL也是正确的~!所以我才很无语啊。。。无语啊。。。无语。。。
  • 打赏
  • 举报
回复
最简单的方法就是打印出来hql,看看是什么,问题就一目了然了
青柠loft 2013-02-28
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
sb.append(" pstate = "+state+" ");引号里少个and
额。。。好细心呀~!但这不是问题的所在呢~!我不走这个state也是查补出来数据的~! 是不是我用的两个session.createQuery的缘故呢??
  • 打赏
  • 举报
回复
sb.append(" pstate = "+state+" ");引号里少个and

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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