Hibernate Learning (vi) HQL

Source: Internet
Author: User

HQL Sentence Learning:

(1) Preliminary understanding of HQL

① HQL is the object-oriented query language, which uses the class name and the property name when querying,and theSql query uses the table name and the field name .

② Case Sensitive

③ Package Name: If the class name is not duplicated, do not write the package name, if the different packages have the same class, then you need to add a package name.

(2) hql Use

A. getting unique values

String hql= "SELECT COUNT (*) from Student";

Query q=session.createquery (HQL);

Number number= (number) Q.uniqueresult ();//Returns a piece of data

System.out.println (Number.intvalue ());

B. getting all the fields of a table

String sqlstring= "from Student";

Query query=session.createquery (sqlString);

List list=query.list ();

for (int i=0;i<list.size (); i++)

{

Student student= (Student) list.get (i);

System.out.println (Student.getname () + "ID:" +student.getid ());

}

C. returning a table section field

String hqlstring= "Select S.sno,s.name from Student S";

Query query=session.createquery (hqlstring);

List<object[]> list=query.list ();

for (int i=0;i<list.size (); i++)

{

Object[] Objects=list.get (i);

System.out.println (objects[0]+ "---" +objects[1]);

}

d. Two table connection queries (e.g., one-to-one relationship)

String hqlstring= "Select C.name,c.addressid.city,c.addressid.country from Companyxml C";

Query query=session.createquery (hqlstring);

List<object[]> list=query.list ();

for (int i=0;i<list.size (); i++)

{

Object[] Objects=list.get (i);

System.out.println (objects[0]+ "---" +objects[1]);

}

E. using map to encapsulate a row of records (alias, equivalent to a key name)

String hqlstring= "Select New Map (c.name as name,c.addressid.city as city,c.addressid.country as country) from Companyxml C ";

Query query=session.createquery (hqlstring);

List<map> list=query.list ();

for (int i=0;i<list.size (); i++)

{

Map Map=list.get (i);

System.out.println (Map.get ("name") + "---" +map.get ("City");

}

F. javabean encapsulation (get partial fields) can be encapsulated by a constructor function

String sqlstring= "Select New Student (S.sno,s.name,s.schoolname) from Student S";

Query query=session.createquery (sqlString);

List list=query.list ();

for (int i=0;i<list.size (); i++)

{

Student student= (Student) list.get (i);

System.out.println (Student.getname () + "ID:" +student.getschoolname ());

}

G. usage criteria query for the WHERE clause

String hqlteststring= "from Student s where s.name=? ";//Search by location, parameter index starting from 0

String hqlteststring= "from Student s where s.name=:name";//dynamic binding is bound by name

Query query=session.createquery (hqlteststring);

Query.setstring ("name", "Ytt");

List list=query.list ();

for (int i=0;i<list.size (); i++)

{

Student student= (Student) list.get (i);

System.out.println (Student.getname () + "ID:" +student.getschoolname ());

}

H. paging query

String sqlstring= "from Student";

Query query=session.createquery (sqlString);

Query.setfirstresult (0);//starting from the first few

Query.setmaxresults (10);//How many bars are displayed on a page

List list=query.list ();

for (int i=0;i<list.size (); i++)

{

Student student= (Student) list.get (i);

System.out.println (Student.getname () + "ID:" +student.getid ());

}

I. inner Connection, outer connection

String hqljoin= "Select S.name,b.picture from Student s, book B";//if using join will cause an exception

Query query=session.createquery (Hqljoin);

List<object[]> list=query.list ();

for (int i=0;i<list.size (); i++)

{

Object[] Object=list.get (i);

System.out.println (object[0]+ "--" +object[1]);

}

(3) Use of SQL statements

Native SQL (Use SQL statements directly in line and database, use sqlquery when querying)

String sqlstring= "Select S.sno,s.name from Student s where S.name=:name";

SQLQuery sqlquery=session.createsqlquery (sqlString);

Sqlquery.setstring ("name", "Ytt");

Sqlquery.addentity (Student.class);//equivalent to from Student, the result can be compared with the data in the Student table, directly through the JavaBean get the property can

List<object[]> list1=sqlquery.list ();

for (int i=0;i<list1.size (); i++)

{

Object[] Object=list1.get (i);

System.out.println (object[0]+ "--" +object[1]);

}


Hibernate Learning (vi) HQL

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.