[Hibernate] Dynamic query via Properties class and HQL statement

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.