读书人

遇到一个非常奇怪的有关问题:session

发布时间: 2013-03-06 16:20:31 作者: rapoo

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


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;
}



第一个段和第三个段代码查出的数据都是正确的,但是第二个确查不出数据,我就郁闷了~!啥情况啊~!求指点
[解决办法]
sb.append(" pstate = "+state+" ");引号里少个and
[解决办法]
后台运行报错吗?debug一下呢?
[解决办法]
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又刚好满足的话,不就语句没结束么。

读书人网 >Java Web开发

热点推荐