Hibernate learning Note (ix)-hibernate query

Source: Internet
Author: User

Hibernate offers the following three ways to search

Hql Search Method:

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

2. Dynamic binding parameters

3. Call the list () method of query to execute the query statement. The method returns a collection of java.util.List types that hold persisted objects that match the query criteria in the list collection.

The 4.Qurey interface supports the method chain programming style, and its setxxx () method returns its own instance

Hibernate's parameter binding mechanism relies on the pre-defined SQL statement functionality of PreparedStatement in JDBC.

Two kinds of forms:

Bind by parameter name: Define the named parameter in the HQL query statement, and the named argument begins with ":". (Hibernate-specific)

Query query = Session.createquery ("From Classes where name=:name1"), Query.setparameter ("name1", "ASDJ");

Bind by parameter location: Define the parameter position with "?" in the HQL query statement

Query query = Session.createquery ("from Classes as C where c.name=?"); Query.setparameter (0, "ASDJ");//alias As, can be omitted

HQL sorting query results with the ORDER BY keyword

Query query = Session.createquery ("from Classes as C order by C.id"); list<classes> Classes = Query.list ();

Paging Query

Setfirstresult (Intfirstresult): 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

Setmaxresult (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

list<classes> Classes = Query.setfirstresult (1). Setmaxresults (5). List ();

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 for an object in a persisted class to wrap the records returned by the projection query

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

From customer C joins c.orders o where O.ordernumber like ' x percent ' if you want the query results to contain only the customer object, use the following form of select C from Customer C join C The. Orders o where o.ordernumber like percent ' Select keyword can also be used to select part of an object's properties Session.createquery ("Select C.id,c.name,o.ordernumber  From CUSTOMERC join C.orders o where o.ordernumber like ' X ") whose corresponding SQL statement is select C.id,c.name,o.order_number from CUSTOMERS C INNER JOIN ORDERS o on c.id = o.customer_id where o.order_number like ' x percent ' filters repeating elements DISTINCT ("Select CreateQuery C.name from Customer C ");
Query query = Session.createquery ("Select                    " New Cn.abc.Customer (C.id,c.name,o.ordernumber) "+                   " from Customer C  join C.orders o  where o.ordernumber like  
Grouping HQL uses the group by keyword to group data and sets constraints on grouped data with the HAVING keyword.

List List=session.createquery ("Select C.name,count (c) from                         the Customer C Group by C.name"). List (); System.out.println (List.size ());

Aggregation Functions

Query query = Session.createquery ("SELECT count (*) from Customer C"), Integer count= (integer) Query.uniqueresult (); System.out.println ("Count" +count); Query query = Session.createquery ("Select AVG (c.age) from Customer C"); float avg= (float) query.uniqueresult (); SYSTEM.OUT.PRINTLN ("avg" +AVG); Query query = session.createquery ("Select Max (c.age), Min (c.age) from  Customer C"), object[] objs= (object[]) Query.uniqueresult (); System.out.println ("Max" + (Long) objs[0]); System.out.println ("min" + (Long) objs[1]); Query query = session.createquery ("Select sum (c.age) from Customer C"); Long sum= (Long) query.uniqueresult (); System.out.println ("sum" +sum);

QBC Search Method

Phrases

Meaning

Restrictions.eq

equals =

Restrictions.alleq

Use map to make multiple equals judgments using Key/value

Restrictions.gt

Greater than >

Restrictions.ge

Greater than or equal to >=

restrictions.lt

Less than <

Restrictions.le

Less than or equal to <=

Restrictions.between

Between clause corresponding to SQL

Restrictions.like

A LIKE clause that corresponds to SQL

Restrictions.in

The IN clause that corresponds to SQL

Restrictions.and

and relationship

Restrictions.or

or relationship

Restrictions.sqlrestriction

SQL qualified Query


Criteria Criteria=session.createcriteria (customer.class);       Set the query criteria, each criterion instance represents a query condition criterion cn1=restrictions.eq ("name", "Tom1"); Criteria.add (CN1); list=criteria.list ();

Use the Help document to see examples

Query methods for Local SQL

query = session.createsqlquery (local SQL statement);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate learning Note (ix)-hibernate query

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.