Five data retrieval methods of Hibernate

Source: Internet
Author: User

We know that Hibernate provides us with a lot of data query methods, such as Hibernate retrieval methods, and we perform the most operations in the project during query, so how can we use Hibernate to retrieve data? Let's take a look at the following:


There are five ways for Hibernate to retrieve data:

1. navigation object graph retrieval method. (Navigate to another object based on the loaded object .)

2. OID retrieval method. (Retrieves an object based on the OID of the object .)

3. HQL retrieval method. (Use the object-oriented HQL query language .)

4. QBC retrieval method. (Use the QBC (Qurey By Criteria) API to retrieve objects .)

5. Local SQL retrieval method. (Use the SQL query statement of the local database .)

1. navigation object graph Retrieval Method

Use the relationship between classes to retrieve objects. For example, to search for an order, you can automatically navigate to the order object to find the customer object to which the order belongs. Of course, the premise is that the multi-to-one relationship must be configured on the object-link ing file.

Order order = (Order) session. get (Order. class, 1 );

Customer customer = order. getCustomer ();

2. OID Retrieval Method

It mainly refers to loading the object corresponding to a record using the get () and load () Methods of the Session.

Customer customer = (Customer) session. get (Customer. class, 1 );

Customer customer = (Customer) session. load (Customer. class, 1 );

3. HQL Retrieval Method

HQL (Hibernate Query Language) is an Object-Oriented Query Language, which is similar to the SQL Query Language. Among the various retrieval methods provided by Hibernate, HQL is the most widely used retrieval method. It has the following features:

Set various query conditions in the query statement.

Supports projection query, that is, only partial attributes of an object are retrieved.

Supports querying by page.

Supports group query and supports the group by and having keywords.

Provides built-in Aggregate functions, such as sum (), min (), and max ().

Can Call User-Defined SQL functions.

Supports subqueries, that is, nested queries.

Supports dynamic parameter binding.

The Qurey interface of the Session class supports HQL retrieval. It provides various query functions listed above.

Note: The Qurey interface supports the method chain programming style. Its set method returns its own instance instead of the void type. The method chain programming style makes the program code more concise.

Sample Code:

Query query = session. createQuery ("from Customeras c where" + "c. name =: customerName andc. age =: customerAge ");

// Dynamically bind the query. setString ("customerName", "Test"); query. setInteger ("customerAge", 21 );

// Execute the search Listresult = query. list ();

// Method chain programming style Listresult1 = session. createQuery ("from Customer as c wherec. name =: customerName and c. age =: customerAge "). setString ("customerName", "Test "). setInteger ("customerAge", 21 ). list ();

4. QBC (Qurey ByCriteria) Retrieval Method

When using HQL retrieval, you must define a string-based HQL query statement in the application. The qbc api provides another method for retrieving objects. It consists of the Criteria interface, Criterion interface, and Expression class. It supports dynamic generation of query statements at runtime.

Sample Code:

Criteria criteria = session. createCriteria (Customer. class );

Criterion criterion1 = Expression. like ("namr", "T % ");

Criterion criterion2 = Expression. eq ("age", newInteger (21 ));

Criteria = criteria. add (criterion1 );

Criteria = criteria. add (criterion2 );

// Execute the search Listresult = criteria. list ();

// Method chain programming style Listresult1 = session. createCriteria (Customer. class). add (Expression. like ("namr" "T %"). add (Expression.

Eq ("age", new Integer (21). list ();

Hibernate also provides the QBE (Qurey By Example) retrieval method, which is a sub-function of QBC. QBE allows you to create an on-demand template first, and then retrieve the same objects as this template.

Sample Code:

Customer exampleCustomer = new Customer ();

ExampleCustomer. setAge (21 );

List result1 = session. createCriteria (Customer. class). add (Example. create (exampleCustomer). list ();

QBE is not very powerful and only useful in some scenarios. A typical application scenario is to allow users to enter a series of query conditions in the query window and then return matched objects. Currently, the QBE method only supports fuzzy match between the same value of the object attribute field and the string, and does not support operations such as interval or greater. In these cases, the HQL or QBC retrieval method is used.

5. Local SQL Retrieval

When HQL or QBC is used for retrieval, Hibernate generates standard SQL query statements, which are used on all database platforms. Therefore, both retrieval methods are cross-platform. Some applications may need to generate some special query statements based on the SQL dialect of the underlying database. In this case, you can use the SQL retrieval method provided by Hibernate.

Sample Code:

Query query = session. createSQLQuery ("select {c. *} from CUSTOMER as cwhere c. NAME like: customerName andc. AGE =: customerAge ");

// Dynamically bind the query. setString ("customerName", "Test"); query. setInteger ("customerAge", 21 );

// Execute the search List result = query. list ();

We have seen five retrieval methods.

Related Article

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.