Hibernate Learning (8): Retrieval Method

Source: Internet
Author: User

Hibaworkflow provides a wide range of search methods. Because ibatis is basically used for queries in projects, especially complex queries, it has never been used by hibaworkflow. In general, Hibernate provides the following centralized query methods:

1. retrieve a specified Record Based on the oId. Typical methods include session. Load () and session. Get ();

2. hql retrieval method. Hql is written by Hibernate query language, similar to SQL statements, only

However, it is object-oriented. To some extent, you can say that hql is an object-oriented query statement, while SQL is a relational query statement. Session. createquery (string hql) is a typical retrieval method. In hibernate2.0, another session. Find method also uses this method, but it has been eliminated;

3. QBC retrieval method. Use the criteria API to retrieve objects. It encapsulates string-Based Query statements and provides more object-oriented interfaces.

4. Native SQL retrieval. Use the SQL query statement of the local database. Hibernate maps the retrieved JDBC resultset result set to a persistent object graph. Typical methods include session. createsqlquery (string SQL );

The first retrieval method is very common and simple. The following mainly describes the last three retrieval methods.

I. hql Retrieval Method

CodeThe snippets are as follows:

......................................................................................................

Query query = session. createquery ("from customer as c Where C. Name =: Name ");

 

Query. setstring ("name", "cmtober ");

 

List result = query. List ();

......................................................................................................

A typical hql search can be divided into three steps:

1. Create a query-type object that contains an hql statement. For example, if the above Code is red, note that from is followed by the class name rather than the table name;

2. dynamically bind parameters (if dynamic parameters are used ). Two methods are available for dynamic binding of parameters: name parameter binding and location parameter binding. The name parameter used in the preceding example is bound in the form of "colon: parameter name ". Another type of Location Parameter binding is as follows:

Query query = session. createquery ("from customer as c Where C. Name = ?");

Query. setstring (0, "cmtober ");

This method is similar to the pre-Compilation Method in JDBC. When both the named parameters and the location parameters are used, the location parameters must appear before the naming parameters; otherwise, an error occurs. The following code:

Query query = session. createquery ("from customer as c Where C. Name =: Name and C. Sex = ?");

Query. setstring (0, "F ");

Query. setstring ("name", "cmtober ");

The following error occurs during running: cannot define positional parameter after any named parameters have been defined.

3. Call the list () method to execute the query statement.

Ii. QBC Retrieval Method

The code snippet is as follows:

......................................................................................................

Criteria = session. createcriteria (customer. Class );

 

Criterion criterion1 = expression. eq ("sex", "F ");

Criterion criterion2 = expression. Like ("name", "% SB1 % ");

Criteria. Add (criterion1 );

Criteria. Add (criterion2 );

 

Result List = criteria. List ();

......................................................................................................

A typical QBC can be divided into the following steps:

1. Create a criteria type object. The red code above is shown. Here we can see that QBC is object-oriented and obviously does not support multi-table joint queries, because it is for a persistence class.

2. Specify the query conditions, as shown in the green code above. Each object of the criterion type represents a query condition. All objects of the criterion type are added to criteria. criteria can be regarded as a set of criterion. In English, criteria is the plural form of criterion, so let's take a look at E.ProgramIt is still helpful, ^ _ ^. The expression class is used to specify query conditions. It inherits from the restrictions class and defines many methods to set query conditions. For details, see its API documentation.

3. Call the list () method to execute the query statement.

Iii. Native SQL Retrieval)

A typical code is as follows:

......................................................................................................

Query query = session. createsqlquery ("selct * From customors where name =: Name ");

Query. setstring ("name", "cmtober ");

 

List result = query. List ();

......................................................................................................

 

This retrieval method is very similar to the hql retrieval method, except that the query statement style is different, which is not described in detail.

 

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.