日期:2014-05-17  浏览次数:20961 次

Hibernate多条件查询如何做
我想用Hibernate做多条件查询前台传来Map条件对一些条件可有可无我这样写的但是看着就不对请问高手应该怎么做到
String hql = "select a.houseid as houseid,a.buildunitid as buildingid,a.buildingname as buildingname,"
+ " f.unitid as sellerunitid,f.unitname as sellerunitname,e.unitid as servicerid,e.unitname as"
+ " servicername,b.sectionid as sectionid,b.sectionname as sectionname,c.unitid as streetid,"
+ " c.unitname as streetname,d.villageid as villageid,d.villagename as villagename,a.cellid as"
+ " cellid,a.cellname as cellname, a.roomno as roomno,a.payarea as payarea,a.actarea as actarea,"
+ " a.telphone as telphone,a.mobilephone as mobilephone,a.location as location,"
+ " g.keyvalue as structtype,g.keyname as structtypename,"
+ "h.keyvalue as buildtype,h.keyname as buildtypename,"
+ "i.keyvalue as housetype,i.keyname as housetypename,"
+ "j.keyvalue as houseusertype,j.keyname as houseusertypename,"
+ "k.keyvalue as paymentflag,k.keyname as paymentflagname,"
+ "l.keyvalue as housestatus,l.keyname as housestatusname"
+ " from v_arh_houseinfo a"
+ ", arh_section b,arh_streetoffice c"
+ ",arh_villageinfo d "
+ ",arh_servicerinfo e"
+ ",arh_sellerunitinfo f "
+ ",(select * from dic_constants u where u.constantcategoryid=1091) g "
+ ",(select * from dic_constants v where v.constantcategoryid=1092) h "
+ ",(select * from dic_constants w where w.constantcategoryid=1093) i "
+ ",(select * from dic_constants x where x.constantcategoryid=1094) j "
+ ",(select * from dic_constants y where y.constantcategoryid=1120) k "
+ ",(select * from dic_constants z where z.constantcategoryid=1090) l "
+ "where 1=1 and a.sectionid=b.sectionid(+) and a.villageid=d.villageid(+) "
+ " and a.streetid=c.unitid(+) and a.servicerid=e.unitid(+) and a.sellerunitid=f.unitid(+) "
+ " and a.structtype=g.keyvalue(+) and a.buildtype=h.keyvalue(+) and a.housetype=i.keyvalue(+) "
+ " and a.houseusetype=j.keyvalue(+) and a.paymentflag=k.keyvalue(+) and a.housestatus=l.keyvalue(+) "
+ "and a.structtype=:structtype and a.buildtype=:buildtype and a.housetype=:housetype" 
+" and a.paymentflag=:paymentflag order by a.houseid desc";
SQLQuery q = session.createSQLQuery(hql);
q.setProperties(item);
q.setFirstResult(startRow).setMaxResults(pageSize);

------解决方案--------------------
只能说方法有问题,,, select * from dic_constants u where u.constantcategoryid=1091) g "
+ ",(select * from dic_constants v where v.constantcategoryid=1092) h "
+ ",(select * from dic_constants w where w.constantcategoryid=1093)
 有 where in可能代替!!
------解决方案--------------------
把这些分开了,做条件判断,if(null!=map中对应参数)
hql += "and a.structtype=:structtype ";

if(null!=map中对应参数)
hql += "";

...不知道是你那个意思吗