HQL simple several methods with conditional query _hibernate

Source: Internet
Author: User

Conditional query
* Can be used to pass parameters in the form of a spelling string
* Can be used. To pass the argument (the index starts at 0)
* Can be used: parameter name to pass parameters
* If you pass multiple parameters, you can use the Setparamterlist method
* Functions of the database can be used in hql, such as: Date_format

The first type: you can spell strings
List students = Session.createquery ("Select S.id, s.name from Student s where s.name like '%1% '"). List ();

The second type: you can use. Way to pass parameters

Index of parameters starting from 0
The parameter values passed are not enclosed in single quotes
Note Method Chain programming
List students = session.createquery ("Select S.id, s.name from Student s where s.name like?")
. Setparameter (0, "%1%")
. List ();

The third type: parameter names are passed by parameter values
List students = session.createquery ("Select S.id, s.name from Student s where S.name like:myname")
. Setparameter ("MyName", "%1%")
. List ();

The fourth kind: The parameter name way passes the parameter value

List students = session.createquery ("Select S.id, s.name from Student s where S.name like:myname and S.id=:myid")
. Setparameter ("MyName", "%1%")
. Setparameter ("myID", 12)
. List ();

Fifth: Support in, need to use setparameterlist for parameter passing

List students = session.createquery ("Select S.id, s.name from Student s where S.id in (: Myids)")
. Setparameterlist ("Myids", New Object[]{1, 2, 3, 4, 5})
. List ();

Sixth: The functions of the database can be used in hql, such as: Date_format

Search for students created in February 2010
List students = session.createquery ("Select S.id, s.name from Student s where Date_format (S.createtime, '%y-%m ') =?")
. Setparameter (0, "2010-02")
. List ();

The seventh kind: between SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

 &NBSP
   //query for students created from 2010-01-10 to 2010-02-15
   list students = Session.createquery ("Select S.id, s.name from Student s where s.createtime between?") and? ")
         .setparameter (0, Sdf.parse ("2010-01-10 00:00:00"))
         .setparameter (1, Sdf.parse ("2010-02-15 23:59:59"))
               .list (

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.