Hibernate Query Language

Source: Internet
Author: User

The HQL (Hibernate query Language) Query language is a fully object-oriented query language that provides a more object-oriented encapsulation that understands concepts such as polymorphism, inheritance, and association. HQL looks similar to SQL statements, but it provides more powerful query functionality.

1. Understanding HQL Language

HQL statements are similar to SQL statements, and their basic usage habits are the same as SQL. Because HQL is an object-oriented query language, it says that it needs to query information from the target object and return a collection that matches a single Entity object or multiple entity objects, while the SQL statement looks for the specified information from a database table, returning a collection of individual or multiple pieces of information.

Note: HQL statements are case-sensitive, although SQL statements are not case-sensitive. Because HQL is an object-oriented query statement, its query target is the entity object, that is, Java class, Java class is case-sensitive, so HQL is also case-sensitive.

The basic syntax for HQL is as follows:

Select "object. Property name"  from "Object" where "Filter Conditions" Group  by  having "Grouping conditions" Order  by "Object. Property name"
Example 1.1 in the actual application of the HQL statement.
Select *  from where emp.flag="1"

The statement is equivalent to:

 from where emp.flag="1"

Where employee is the entity object. Hibernate can use HQL to perform update and delete operations after version 3.0, but it is not recommended in this way.

2. Entity Object Query

In the HQL statement, the entity object can be queried directly by the FROM clause.

Example 2.1 querying entities through the FROM clause.
 from Person

In most cases, it is a good idea to specify an alias for the entity object of the query, which makes it easier to reference the entity object elsewhere in the query statement. The aliases are named in the following ways:

 from Person per
Tip: The first letter of the alias is best lowercase, which is the canonical notation of the HQL statement, consistent with the naming conventions of variables in Java, and avoids confusion with entity objects in the statement.

The HQL statement above will query all data corresponding to the entity object person in the database and return it as a collection of encapsulated person objects.

Example 2.2 queries the specified field data through the FROM clause.
Select  from Person per

This way of querying, the entity object is dynamically instantiated by the New keyword, and the specified entity object properties are repackaged, without losing the encapsulation of the data and improving the efficiency of the query.

Note: It is best not to use the following statement for querying in the above statement, for example:
Select  from Person per
Because this statement returns not the original object entity state, it is an array of type object that destroys the original encapsulation of the data.

See the following links for more details:

Hibernate HQL,QBC Query Language hibernate query Language HQL eight main points

Hibernate Query Language

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.