Hibernate Query method

Source: Internet
Author: User

Hibernate Query method

1 Object Navigation Query

(1 ) by ID query a customer, and then query all the contacts in this customer

2 OID Enquiry

(1 ) by ID querying a record, returning an object

3 HQL Enquiry

(1 ) Query object, write Hql Statement Implementation Query

4 QBC Enquiry

(1 ) Criteria Object

5 Local SQL Enquiry

(1 ) SQLQuery object, using plain SQL Implementing Queries

Object Navigation Query

1 query all contact procedures within a customer, using object navigation to implement

2 Code

OID Query

1 by ID Query Records

(1 ) Call Session inside the Get Method Implementation

HQL Query

1 hql : Hibernate query Language , Hibernate provides a query language that is similar to the HQL language and plain SQL , with the difference: Normal SQL Manipulating database tables and fields, HQL action entity classes and properties

 

2 Common HQL statements

(1 ) query all: from entity class name

(2 ) conditional query: From entity class name where property name =?

(3 ) sort query: From entity class name ORDER by entity class Property name Asc/desc

 

3 Use the HQL query operation, use Query Object

(1 ) Create a query object, write hql statement

(2 ) Call the method inside the query object to get results

Query all

1 query all customer records

(1 ) to create a query object, write Hql Statement

(2 ) Call Query the method inside the object gets the result

2 query all: from entity class name

Conditional query

1 hql conditional Query Statement notation:

(1 ) from entity class name where entity class Property name =? and entity class Property name =?

from entity class name where entity class Property name like?

2 Code

Fuzzy query

Sort queries

1 hql Sort Statement Notation

(1 ) from entity class name Order by entity Class Property name Asc/desc

Paging Query

1 MySQL Implementing Paging

(1 ) using the keyword limit Implement

2 in HQL implementing paging in

(1 ) in HQL operation, limit is not written in the statement , Hibernate the query object encapsulation Two methods for paging operations

Projection Query

1 projection Query: The query is not all field values, but the values of the partial fields

2 projection Query hql sentence notation:

(1 ) Select entity Class Property name 1, entity Class Property name 2 from entity class name

(2 ) Select can't write in the back * , not supported by

3 Specific Implementation

The aggregation function uses

1 Common aggregation Functions

(1 ) Count , Sum , Avg , Max , Min

2 hql aggregation Function Statement notation

(1 ) query table record Count

-Select COUNT (*) from entity class name

QBC Query

1 using HQL query requires write HQL statement implementation, but using QBC time, do not need to write the statement, the use of methods to implement

2 using QBC , manipulate entity classes and properties

3 using QBC , use criteria Object Implementation

Query all

1 Create Criteria Object

2 call method to get results

Conditional query

1 no statements, using encapsulated methods to implement

Sort queries

Paging Query

Start position Calculation formula: (Current page -1 ) * number of records per page

Statistics Query

Offline query

1 servlet Call Service , service Call DAO

(1 ) in DAO inside the database crud Operation

(2 ) in DAO use hibernate inside framework, using Hibernate Frame, call session inside the method to implement the function

(3 ) in the back SSH specific application in practice

HQL Multi-table query MySQL inside multi-table query


HQL implementing multi-table Queries  

HQL Multi-Table Query

(1 ) Inner Connection

(2 ) left outer connection

(3 ) Right outer connection

(4 ) Urgent internal connection

(5 ) Urgent left outer connection

HQL Internal Connection

1 INNER JOIN query hql sentence wording: Take accounts and contacts as an example

(1 ) from  customer  c  inner  join  c. Setlinkman

 

 

returns list , list Each part is an array form

 

 

 

2 to demonstrate urgent internal connections

(1 ) urgent internal connection and inner join the bottom implementation of the same

(2 ) Difference: Use an inner connection to return each part of the list to be an array, eager to connect back to list each part is an object

(3 ) HQL statement Syntax

-from  customer  c  inner  join  fetch  c. Setlinkman

 

 

HQL LEFT Outer connection

1 left outer connection hql statement:

(1 ) from the Customer C left outer join c. Setlinkman

(2 ) Urgent left outer connection from Customer C ieft outer join fetch c. Setlinkman

2 LEFT OUTER join returns list Each part is an array, and an urgent left OUTER join returns the list each part is an object

1 right outer connection hql statement:

(1 ) from the Customer C right outer join c. Setlinkman

The concept of Hibernate retrieval policy retrieval Strategy

1 Hibernate Search strategies fall into two categories:

(1 ) query now: Based on ID query, call get method, a call to get method to send the statement query database immediately

(2 ) deferred query: based on ID query, and load method, call load The method does not send the statement query data immediately, only send the statement query database when the value inside the object is obtained.

2 deferred queries fall into two categories:

(1 ) class-level delay: Based on ID the query returns the entity class object, calling the load method does not send the statement immediately

(2 ) Association level delay:

- Querying a customer and then querying all the contacts of the customer, whether the process of querying all of the customer's contacts requires a delay, a process called association level delay

Association level Deferred Action

1 Configuring the implementation in a mapping file

(1 according to the customer, all contacts are configured in the customer mapping file .

2 in the Set use attributes on labels

(1 ) Fetch : Value Select (default)

(2 ) Lazy : Value

-True : Delay (default)

-False : No delay

-Extra : Extremely late

(1 ) Call Get after that, send two SQL Statement

(1 ) Extremely lazy, what value to give what value

Batch Crawl

1 query all customers, return to list collection, traversing the list collection, get each customer, get all the contacts for each customer

(1 ) The above operation code, send more than one SQL Statement

2 in the customer's mapping file, the set Label Configuration

(1 ) Batch-size value, the higher the value, the less the sending statement

Hibernate 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.