Hibernate HQL Query Introduction

Source: Internet
Author: User
Tags aliases join

The traditional SQL language uses structured query methods that are powerless to query data that exists in the form of objects. Fortunately, Hibernate provides us with a syntax-like SQL language, Hibernate query Language (HQL), unlike SQL, HQL is an object-oriented query language that can query data that exists in the form of objects. Therefore, this article on how to hql how to work and how to use HQL to launch an in-depth discussion.

The SQL itself is very powerful. HQL is generated when this powerful SQL is combined with the ability to handle object-oriented data. Like SQL, HQL provides rich query functionality such as projection queries, aggregate functions, grouping, and constraints. Any complex SQL can be mapped to HQL.

The first part of this article discusses the simple use of hql. The second section discusses how to query in HQL based on contextual relationships. In the third part, an example will be given to illustrate how to use HQL in practical applications.

Enter the HQL world

An ORM framework is built on an object-oriented basis. The best example is hibernate how to provide a class SQL query. Although HQL's syntax is similar to SQL, its query target is actually an object. HQL has all the attributes of an object-oriented language, including polymorphism, inheritance, and composition. This is equivalent to an object-oriented SQL, and in order to provide more powerful functionality, HQL also provides a number of query functions. These functions can be grouped into four categories:

1. Projection function

2. Constraint function

3. Aggregate function

4. Grouping functions

Use HQL to create simple queries, or to create more complex queries. The very complex queries, such as queries that contain subqueries and many connections, are not discussed in this article. This article only discusses queries that connect two tables. Now let's get close to HQL!

Projection

A predicate projection is a property of an object or object that can be accessed. In HQL, you can use the From and select clauses to do this work.

The FROM clause returns all instances of the specified class. If the from order returns all instances of the Order class. In other words, the above query is equivalent to the following SQL statement:

select * from order

From is the simplest query clause. From the following can be followed by one or more class names (class names can also be aliases). To get all the instances of order and product, you can use the following query:

from Order, Product

As with the class name, aliases can be used after from, as shown in the following code:

from Order as o, Product p

When a query is complex, adding an alias can reduce the length of the statement. We can look at the following SQL statements:

select o.*, p.* from order o, product p where o.order_id = p.order_id

We can easily see that the above query is a one-to-many relationship. In HQL is equivalent to an instance of a class that contains more than one other class. Therefore, the above SQL written HQL is:

from Order as o inner join o.products as product

Now let's consider another case where you get the specified property from the table. This is the most commonly used SELECT clause. This works the same way in hql as in SQL. In HQL, a SELECT statement is the last choice if you just want to get the attributes of a class. The above SQL can be changed to the following HQL statement using the SELECT clause:

select product from Order as o inner join o.products as product

The above HQL statement returns all the products instances in the order. If you want to get a property of an object, you can write the HQL statement in the following form:

select product.name from Order as o inner join o.products as product

If you want to get the properties of multiple objects, you can write the HQL statement in the following form:

select o.id, product.name from Order as o inner join o.products as product

Next, we'll move on to the next issue. Suppose we need to get the data according to certain conditions. Then the HQL statement described above will not meet the requirements. To do this, we need to use the CONSTRAINT clause that we will discuss below.

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.