Hibernate Search Method-HQL (7)

Source: Internet
Author: User

Hibernate provides the following ways to retrieve objects

(1) Navigation object Graph retrieval method: Navigating to other objects based on objects that have already been loaded

(2) OID retrieval method: Retrieving objects According to the OID of the object

(3) HQL retrieval method: Using object-oriented HQL query language

(4) QBC retrieval method: Use the QBC (Query by Criteria) API to retrieve the object. This API encapsulates query statements based on string forms and provides a more object-oriented query interface.

(5) Local SQL retrieval method: SQL query statement using local database

HQL (Hibernate query Language) is an object-oriented query language that is somewhat similar to the SQL query language. Among the various retrieval methods provided by Hibernate, HQL is the most widely used method of retrieval. It has the following functions:

(1) Set various query conditions in the query statement

(2) Support projection query, that is, only the partial properties of the object are retrieved

(3) Support paging query

(4) Support connection query

(5) Support for group queries, allowing the having and group by keywords to be used

(6) provides built-in aggregation functions such as SUM (), Min (), and Max ()

(7) Support sub-query

(8) Support dynamic binding parameters

(9) Ability to invoke user-defined SQL functions or standard SQL functions

HQL retrieval methods include the following steps:

(1) Create a query object by using the CreateQuery () method of the Session, which includes a HQL lookup statement. HQL query statements can contain named parameters

(2) Dynamic binding parameters

(3) Call the query-related method to execute the inquiry statement.

The Qurey interface supports the method chain programming style, and its setxxx () method returns its own instance rather than the void type

HQL vs SQL:

1.HQL query statements are object-oriented, Hibernate is responsible for parsing HQL query statements, and then according to the object-relational mapping file mapping information, the HQL query statements translated into the corresponding SQL statement. HQL the body in a query statement is a property of a class and class in a domain model

A 2.SQL query statement is bound to a relational database. The principal in the SQL query statement is the field of the database table and table.

Binding parameters:

The 1.Hibernate parameter binding mechanism relies on the PreparedStatement predefined SQL statement functionality in the JDBC API.

The 2.HQL parameter binding is made up of two forms:

(1) binding by parameter name: Defines a named parameter in the HQL query statement, and the named argument begins with ":".

(2) binding by parameter location: Define the parameter position with "?" in the HQL query statement

3. Related methods:

  (1) setentity (): Binds a parameter to a persisted class

(2) Setparameter (): Binds any type of parameter. The third parameter of the method explicitly specifies the Hibernate mapping type

HQL sorting query results with the ORDER BY keyword

Paging query:

1.setFirstResult (int firstresult): Sets the object from which to start the retrieval, the parameter Firstresult represents the index position of the object in the query results, and the starting value of the index position is 0. By default, query starts the search from the first object in the query results

2.setMaxResults (int maxResults): Sets the maximum number of objects retrieved at one time. By default, the query and Criteria interface retrieves all the objects in the results

To define a named query statement in a mapping file

Hibernate allows you to define query statements in the form of strings in a mapping file.

The <query> element is used to define a HQL query statement, which is tied to the <class> element.

The Getnamedquery () method of the Session is used in the program to get the query object corresponding to the queried statement.

Projection query:

Projection query: The query results contain only part of the attributes of the entity. Implemented by the SELECT keyword.

The query's list () method returns a collection containing elements of the array type, each of which represents a record of the query results

You can define a constructor of an object in a persisted class to wrap the records returned by the projection query so that the program code can use the object-oriented semantics to access the query result set completely.

The DISTINCT keyword can be used to ensure that query results do not return duplicate elements

Report Query

Report queries are used to group and count data, and as with SQL, HQL uses the group by keyword to group data and set constraints on grouped data with the HAVING keyword.

The following aggregation functions can be called in the HQL query statement: Count () min () max () sum () AVG ()

HQL (urgent) left outer connection

The left JOIN FETCH keyword indicates an urgent right-side connection retrieval strategy.

The list () method returns a reference to the collection that holds the entity object, and the employee collection associated with each Department object is initialized to hold all the associated employee entity objects.

Duplicate elements may be included in query results, and duplicate elements can be filtered through a HashSet

Left Outer connection:

The LEFT JOIN keyword indicates an out-of-the-field connection query.

The collection returned by the list () method holds the object array type

Determines the retrieval strategy for the Employee collection based on the configuration file.

If you want the list () method to return a collection that contains only Department objects, you can use the SELECT keyword in a hql query statement

HQL (urgent) Internal connection

The INNER join FETCH keyword indicates an urgent inner join, or you can omit the INNER keyword

The list () method returns a reference to the collection that holds the Department object, and the employee collection for each Department object is initialized to hold all the associated employee objects

Internal connection:

The INNER join keyword indicates an inner join, or you can omit the INNER keyword

Each element in the collection of list () methods corresponds to a record of the query result, each element is an object array type

If you want the returned collection of the list () method to contain only Department objects, you can use the SELECT keyword in a HQL query statement

Retrieval policy at the association level runtime

If you do not explicitly specify a retrieval policy in HQL, the mapping file is used to configure the retrieval policy.

HQL ignores the urgent left outer connection retrieval policy set in the mapping file, and if you want HQL to adopt an urgent left OUTER join policy, you must explicitly specify it in the HQL query statement

If a retrieval policy is explicitly specified in the HQL code, the retrieval policy configured in the mapping file is overwritten

Hibernate Search Method-HQL (7)

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.