Hibernate main Query method

Source: Internet
Author: User

1.HQL Query

1.1 hql query with no parameters

1.2 with reference hql query ( divided into question mark placeholder and character placeholder two kinds )

Ps: use setparameter () binding parameters when binding parameters of various types , such as loop-bound parameters when using indeterminate parameters after encapsulation method, or you can use setproperties () dynamic binding, but not as Criteria Enquiry

2. Special hql Query method

2.1 uniqueresult () query unique results

It is important to note that when the query results are not unique, the nonuniqueresultexception error is reported and the method is used with caution

2.2 Paging Query

2.3 Projection Query

The return value is the Object Collection, and the subscript corresponds to the order of the attribute values in the statement

2.4 to encapsulate query results into objects through a constructor function

String hql = "Select New EMP (empname,salary) from EMP";

You must have this constructor in the Emp class, or create a new JavaBean

3. Native SQL queries

Using the createsqlquery () method, the method parameter is a native SQL statement and provides a addentity () method to map the relational data in the query result set to an object, otherwise The collection of object data is used to receive

4. Named queries

4.1 named queries for HQL query statements

Use the <query> element to define the HQL statement in the mapping file, parallel to the <class> element

Call the getnamedquery () method when used, the parameter is the value of the name attribute in the <query> element , And can be chained to set parameters

4.2 named queries for SQL query statements

The SQL statement wraps around the <sql-query> element, adding a return element,alias property is the table-alias of the operation, andtheclass attribute represents the mapped entity class

Use the same getnamedquery () method when calling

5.Criteria Query

5.1 Use steps

<a> Create a Criteria object using the createcriteria () method of the Session interface

<b> use The static methods provided by the restrictions class to set the query conditions, which return the Criterion object, a Criterion object represents a query condition. the Add () method of the criteria interface is used to add query criteria, or you can set paging

<c> Execute Query statement using the list () method of the Criteria interface

various static methods of 5.2 restrictions class

(1) EQ (String propertyname,object value) equals

(2) NE (String propertyname,object value) is not equal to

(3) GT (String propertyname,object value) greater than

(4) GE (String propertyname,object value) greater than or equal to

(5) LT (String propertyname,object value) is less than

(6) Le (String propertyname,object value) is less than or equal to

(7) IsNull (String propertyname) equals null value

(8) Isnotnull (String PropertyName) is not equal to a null value

5.3 Query Range keywords

(1) in (String propertyname,collection values) or

In (String propertyname,object[] values) equals one of the values in the list

(2) not in is not equal to any one of the values in the list

(3) Between (String PropertyName, Object Low,object High)

Greater than or equal to low value and less than or equal to High

(4) Not Between (String PropertyName, Object Low,object High)

Less than the low value or greater than high

(5) Like/ilike (String propertyname,object value) or

Like/ilike (String propertyname,object value,matchmode matchmode)

Match based on string and match pattern

Match pattern:start starts with a string

End ends with a string

Anywhere string appears in any location

Exact exactly match

5.4 Connection Query ( only internal connections and urgent left outer connections are supported )

Hibernate main Query method

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.