Hibernate (9) HQL query and hibernatehql Query

Source: Internet
Author: User

Hibernate (9) HQL query and hibernatehql Query
I. query methods provided by Hibernate

  • OID query method: Primary Key query. The get () or load () method is used to load the specified OID object and the query result is
  • HQL Query method: Query using the HQL language through the Query interface
  • QBC query method: queries by using interfaces and classes such as Criteria
  • Local SQL query method: use the native SQL language for query
  • Object navigation query method: navigate to its associated object through the loaded object

HQL and QBC are the professional query methods provided by Hibernater.

The HQL query method is the standard query method officially recommended.

Ii. HQL query description

HQL:Hibernate Query Language

This language looks like SQL. But do not be confused by syntax-structure similarity. HQL is consciously designed as a fully Object-Oriented Query That can understand concepts such as inheritance, polymorphism, and association.

For example:

// 1. Get session Session session = HibernateSessionFactory. getSession (); // 2. Create a query statement where Student is a persistent class. The class name is String hql = "from Student"; // String hql = "from Student where sname = 'zhangsan '"; // 3. create a Query object Query = session. createQuery (hql); // 4. the query result is a list set List <Student> list = query. list (); // 5. output result for (Student stu: list) {System. out. println (stu. getSname () + stu. getSex ());}
Iii. Simplest Query

Query all information

From Student // directly use the persistence class name from com. pb. entity. Student // use the full path

Generally, we do not need to use a fully qualified class name, becauseAuto-import(Automatically introduced) is the default situation.

You can also specify an alias for the class name.

From Student as s // use the as keyword to specify the class alias from Student s // omit the as keyword to specify the class alias
4. where clause

The where clause can also be supported.

from Student where sid=201503011from Student  s where s.sid=201503011
4.1. You can use various operators in the where clause

For example:

From Student where sex = 'male' and sid <11070200 // query if the Student gender is male, students whose Student ID is less than 11070200 from Student where sid between 11070100 and 11070200 // query students whose Student ID is between 11070100 and 11070200 from Student where snamelike '% fei '; // query all students whose last word is Fei from Student where sname like '_ ik *'; // query the Student name. The first word is arbitrary, 2nd students of any length after I and 3rd students of k

"-" Matches a single character.

"%" Matches any length of Characters

"_" Cannot match Chinese characters, "%" can match Chinese Characters

5. Returned results

Query Method

5.1. list () method
// 1. Get session Session session = HibernateSessionFactory. getSession (); // 2. Create a query statement where Student is a persistent class. The class name is String hql = "from Student"; // String hql = "from Student where sid = 201503011"; // 3. create a Query object Query = session. createQuery (hql); // 4. the query result is a list set List <Student> list = query. list (); // 5. output result for (Student stu: list) {System. out. println (stu. getSname () + stu. getSex ());}
5.2 iterate () method
// 1. Get session Session session = HibernateSessionFactory. getSession (); // 2. Create a query statement where Student is a persistent class. The class name is String hql = "from Student"; // String hql = "from Student where sid = 201503011"; // 3. create a Query object Query = session. createQuery (hql); // 4. the query result is Iterator <Student> iterator = query. iterate (); // 5. output result while (iterator. hasNext () {Student stu = iterator. next (); System. out. println (stu. getSname () + stu. getSex ());}
5.3 uniqueResult () method

The returned result is an object, which is generally used to query data based on the primary key.

// 1. Get session Session session = HibernateSessionFactory. getSession (); // 2. Create a query statement where Student is a persistent Class, Class Name // String hql = "from Student"; String hql = "from Student where sid = 201503011"; // 3. create a Query object Query = session. createQuery (hql); // 4. the returned result is of the Object type. Student student = (Student) query. uniqueResult (); // 5. output result System. out. println (student. getSname () + student. getSex ());
5.4 differences between list () and iterate () Methods

The query mechanisms of both are different.

When using list (), you only need to query all persistent classes once to query all fields.

When iterate () is used, the primary key ID of all records is queried for the first time, and then the corresponding records are queried in the system cache. If there is a record with this ID, no database query is performed, if no database query is performed, You need to query more than 1 N records.

6. Property query (projection query) 6.1. directly specify the property Query

The returned result is an array of objects. The length of the array is the number of attributes.

Public static void findByproperty () {// 1. Get session Session session = HibernateSessionFactory. getSession (); // 2. Create a query statement where Student is a persistent Class, Class Name // String hql = "from Student"; String hql = "select sid, sname, sex from Student "; // 3. create a Query object Query = session. createQuery (hql); // 4. the returned results for executing the query are of the Object type, and List list = query. list (); // 5. output result for (Object object: list) {// The Object array Object [] obj = (object []) Object to query attribute data; System. out. println (obj [0] + "\ t" + obj [1] + "\ t" + obj [2]);}
6.2 query using Constructor

The premise is that the constructor must be included in the persistence class.

Sets of objects whose returned results are persistence classes

Public static void findByCons () {// 1. Get session Session session = HibernateSessionFactory. getSession (); // 2. Create a query statement where Student is a persistent Class, Class Name // String hql = "from Student"; String hql = "select new Student (sid, sname, sex) from Student "; // 3. create a Query object Query = session. createQuery (hql); // 4. the query result is of the Object type. List <Student> list = query. list (); // 5. output result for (Student stu: list) {System. out. println (stu. getSname () + "\ t" + stu. getSex ());}}

This kind of method is more direct and clear, and there must be corresponding constructor methods.

VII. Update and delete objects 7.1. Update Session session = new Configuration (). configure (). buildSessionFactory (). openSession (); Student stu = (Student) session. get (Student. class, 201509009); Course cou = (Course) session. get (Course. class, 2222); Transaction tran = session. beginTransaction (); stu. getCourses (). add (cou); session. update (stu); tran. commit (); session. close ();View Code7.2. Delete Session session = new Configuration (). configure (). buildSessionFactory (). openSession (); Student stu = (Student) session. get (Student. class, 201509009); Course cou = (Course) session. get (Course. class, 2222); Transaction tran = session. beginTransaction (); stu. getCourses (). remove (cou); session. update (stu); tran. commit (); session. close ();View Code 8. Bind a 8.1 placeholder to a parameter,

Same as JDBC? To placeholder

8.2. Name Parameters

9. Sorting

10. Statistical functions

 

Public static void findCount () {Session session = new Configuration (). configure (). buildSessionFactory (). openSession ();/* String hql = "from Emp"; Query query = session. createQuery (hql); List list = query. list (); int count = list. size (); */String hql = "select count (*) from Emp"; Query query = session. createQuery (hql); Long count = (Long) query. uniqueResult (); System. out. println ("Total number of records in the employee table:" + count );}View Code

 

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.