//need to ensure that the setter and getter in the EMP and empproperties as well as attributes and parameter placeholders (: ename) are consistent
//Dynamic Query@Test Public voidtest4 () {empproperties EP=Newempproperties (); //set Query conditionsEp.setename ("%a%"); Ep.setstartdate (NewDate (383155200000L)); Ep.setenddate (NewDate ()); Ep.setjob ("Clerk"); //1.obtain org.hibernate.Session object.Session ss=hibernateutil.getsession (); //2.hql statement:sStringBuffer hql=NewStringBuffer ("from Emp e where 1=1"); //3. Determine if the Empproperties total property is null if(Ep.getename ()! =NULL) {hql.append ("and E.ename like:ename");//E.ename and EMP properties remain consistent |:ename and properties in Empproperties } if(Ep.getstartdate ()! =NULL) {hql.append ("And E.hiredate>=:startdate"); } if(Ep.getenddate ()! =NULL) {hql.append ("And E.hiredate<=:enddate"); } if(Ep.getjob ()! =NULL) {hql.append ("And E.job=:job"); } //executes the HQL statement, HQL converts to string (using the ToString () method)Query qr=Ss.createquery (hql.tostring ()); //Assigning a value to a parameter in a HQL statementqr.setproperties (EP); @SuppressWarnings ("Unchecked") List<Emp> el=qr.list (); for(Emp emp:el) {System.out.println (Emp.getename ()+ "\ T" +emp.getempno ()); } System.out.println ("Number of query records:" +el.size ()); Hibernateutil.closeresource (ss); }
[Hibernate] Dynamic query via Properties class and HQL statement