Hibernate retrieving queries in several ways (HQL,QBC, local SQL, integrated spring, etc.)

Source: Internet
Author: User

1. Non-integrated spring

Hibernate's search method, mainly has the following five kinds.

1. Navigation object Graph retrieval method. (Navigate to other objects based on the objects that have already been loaded.) )

2.OID retrieval method. (objects are retrieved according to 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. (SQL query statement using the local database.) )

1. Navigation object Map Retrieval method

Use the relationship between classes and classes to retrieve objects. For example, if we are looking for an order, we can automatically navigate by the order object to find the customer object to which the order belongs. Of course, the premise is that you must configure their many-to-one relationships on the object-relational mapping file.

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

Customer customer = Order.getcustomer ();

2. OId Retrieval method

The main point is to use the get () and load () methods of the session to load the corresponding object of a record.

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

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

3. Hql Search method

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 features:

Set various query conditions in the query statement.

Supports projection queries, which retrieve only a subset of the properties of an object.

Support for paging queries.

Support for grouping queries, allowing the group by and having keywords to be used.

Provides built-in aggregation functions such as SUM (), Min (), and Max ().

Ability to invoke user-defined SQL functions.

Supports subqueries, which are nested queries.

Supports dynamic binding parameters.

The Qurey interface of the session class supports the HQL retrieval method, which provides the various query functions listed above.

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

Example code:

  1. Query query = session.createquery ("from Customer as C where" +"C.name=:customername and C.age=:customerage");
  2. Dynamic binding Parameters
  3. Query.setstring ("CustomerName", "Test");
  4. Query.setinteger ("Customerage", 21);
  5. Perform a search
  6. List result = Query.list ();
  7. Method Chain Programming Style
  8. List result1 = Session.createquery ( "from Customer as C where C.name=:customername and C.age=:customerage"). setString (" customerName", "Test"). Setinteger ("Customerage") . List ();

4. QBC (Qurey by Criteria) Search method

When using HQL retrieval, you need to define HQL query statements based on string form in your application. The QBC API provides another way to retrieve objects, consisting primarily of the criteria interface, the criterion interface, and the expression class, which supports dynamically generating query statements at run time.

Example code:

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); //perform a retrieve List result = Criteria.list (); //method chain Programming Style List RESULT1 = Session.createcriteria (Customer.class). Add (Expression.like ("Namr" "t%")). Add (Expression. EQ ("Age",NewInteger (21)) . List (); Hibernate also provides a QBE (Qurey by Example) retrieval method, which is a sub-function of QBC.  QBE allows you to create a random template and then retrieve the same object as the template. Sample code: Customer Examplecustomer=NewCustomer (); Examplecustomer.setage (21st); List RESULT1= Session.createcriteria (Customer.class). Add (Example.create (Examplecustomer)). List ();

The functionality of QBE is not particularly powerful and is useful only in certain situations. A typical use case is to have the user enter a series of query conditions in the Query window, and then return the matching object. QBE mode currently supports only object attribute fields that are equal to queries and fuzzy matching of strings, cannot support intervals, or greater than such operations. In these cases, the hql retrieval method or QBC retrieval method is used.

5. Local SQL Retrieval method

With HQL or QBC retrieval, Hibernate generates standard SQL query statements that are used for all database platforms, so both of these 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 take advantage of the SQL retrieval provided by hibernate.

Example code:

Query query = Session.createsqlquery ("Select {c.*} from CUSTOMER as C where C.name Like:customername and C.age=:customera GE ");      // Dynamic binding Parameters       Query.setstring ("CustomerName", "Test");      Query.setinteger ("customerage", +);      // perform a   search List result = Query.list ();  

Above we see the application of five kinds of retrieval methods, the most widely used in the actual project is HQL and QBC.

Hibernate retrieving queries in several ways (HQL,QBC, local SQL, integrated spring, etc.)

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.