Hibernate HQL Introduction

Source: Internet
Author: User

HQL (Hibernate Query Language). The traditional SQL language uses structured query methods, which can do little to the data that exists in the form of objects. To do this, Hibernate provides us with a syntax-like SQL language, which is HQL (Hibernate query language), unlike SQL, HQL is an object-oriented query language that can query data that exists as an object.
SQL itself is a very powerful query language. When SQL has the ability to handle object-oriented data, there is a hql. Like the SQL language, HQL provides rich query functionality. such as projection queries, aggregate functions, groupings, and constraints. Any complex SQL can be mapped into hql.

"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "
The ORM Framework is built on an object-oriented basis. The best manifestation is that hibernate provides a class SQL query. Although the syntax of HQL is similar to SQL, its actual query target is the object. So HQL has all the features of the object-oriented language, including polymorphism, inheritance, and composition. In this way, ordinary SQL has object-oriented features, and in order to provide more powerful functions, HQL also provides a lot of query functions.
These functions can be divided into four categories:
1. Projection function
A projection is a property of an object or object that can be accessed. HQL is done by using the From and select clauses.
The FROM clause returns all instances of the specified class. As from user, all instances of the user class are returned. In other words, the corresponding SQL is:

select * from user;

The From is the simplest query statement. The from can be followed by one or more class names (the class name can be aliased), as shown in the following code:

From User as u,rose as R;

2. Constraint function
The projection returns all the data, but in most cases we don't need that much data. Then we need to filter the data. HQL uses the same clause as SQL for filtering data. Its syntax is similar to SQL, as shown in the following code:
HQL statements

Select User as u where u.id = 1978;

SQL statements

Select User  u where u.id = 1978;

The WHERE clause used in the above two statements is very similar, and the only difference is that the SQL operation is a record, and the HQL operation is an object. In HQL, the HAVING clause can also accomplish this function in addition to the WHERE clause to filter the data. Projections and constraints are two basic operations, and these two operations, together with the aggregation function, will become more powerful hql.

3. Aggregation function
All of the above queries treat each record (object) as one unit, and if you use aggregations, you can treat a class of records (objects) as one unit. Then a series of operations are performed on each type of record (object). For example, averaging, summing, counting rows, and so on for a column. HQL Supported aggregation functions:
(1) Avg (...), sum (...)
(2) min (...), Max (...)
(3) Count (*), COUNT (...), COUNT (distinct ...), count (all ...)
We want to count the number of products with the average price less than amount according to the Product table ID, the HQL statement is as follows:

Select COUNT (P) from Product P has p.price < AVG (amount) group by p.id;

4. Grouping functions
The grouping operation is a collection of rows. It groups the recordset based on a column (property). All of this is done through the group clause. The following example describes the general use of the group clause.

Select COUNT (o) from Order O has o.pricetotal >= and o.pricetotal <= 3200 GROUP by O.id

Grouping in HQL is similar to grouping in SQL. In short, except for some special extensions to SQL, all other SQL functions can be described using the HQL.

"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "




Hibernate HQL Introduction

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.