Hibernate connection query Summary

Source: Internet
Author: User

If there are two associated tables, customer and order, generally one customer can correspond to multiple orders, and one order can only correspond to one customer; in order, there will be a customer object as the attribute, and in hbm there will be a role-to-one; in customer, there will be a set <Order>, hbm has one-to-may.

 

Next we will study the query policies and results in various connection modes and summarize them,

 
First, no connection
Hql: from Customer cwhere c. name like "T %"

Note: The preceding statements do not show the order,

Executed SQL: select * from customer wherename like "T %"

The query result is:

Id
Name
Age
 
1
Tom
21
 
5
Tom
25
 

Result: The result contains two customer object elements. They correspond to two data items with id 1 and id 5 respectively, and their order set attributes are not initialized.

 

Second, urgent left Outer Join
Hql: from Customer c leftjoin fetch c. order where c. name like "T %"

Description: displays the order attribute of a specified query person.

SQL statement: select c. C_ID, c. NAME, o. ID, o. ORDER_NUMBER, o. CUSTOMER_ID

From CUSTOMER c left out join ORDER o onc. ID = o. CUSTOMER_ID where (c. NAME like "T % ")

The query result is:

C_ID
NAME
AGE
O_ID
O_NUMBER
CUSToMER_ID
 
1
Tom
21
1
Tom_order001
1
 
1
Tom
21
2
Tom_order002
1
 
1
Tom
21
3
Tom_order003
1
 
5
Tom
25
Null
Null
Null
 

Result: Based on the left table, query the order of all users starting with T. When a user does not have an order, the order attribute is filled with null.

(Note: hibernate allows A query to be connected to multiple join tables on the left and on the right, one-to-one or one-to-one. For example, table A has A field bId and one field cId; there is a B table and A C table, and the IDs of the two are the attributes of A respectively. When they perform an urgent left outer join, the statement is: from A a left join fetch a. B left join fetch a. c where B is notnull and c is not null. Or in the second case, if Table A contains bId, table B contains cId, and Table C is not associated with other tables, their statement is: from A a left join fetch. B left join fetch B. c where B is notnull and c is not null)

 

Third, left Outer Join
Hql: from Customer c left join c. orderwhere c. name like "T %"

Description: displays the order attribute of a specified query person.

Executed SQL: select c. ID C_ID, c. NAME, c. AGE, o. ID O_ID, o. ORDER_NUMBER, o. CUSTOMER_ID from CUSTOMER c left outer join ORDERS o on c. ID = o. CUSTOMER_ID where (c. NAME like't % ')

The query result is:

C_ID
NAME
AGE
O_ID
ORDER_NUMER
CUSTOMER_ID
 
1
Tom
21
1
Tom_order001
1
 
1
Tom
21
2
Tom_order002
1
 
1
Tom
21
3
Tom_order003
1
 
5
Tom
25
Null
Null
Null
 

 

 

Fourth, internal connection
Hql: from Customer c inner join c. orders owhere c. name like "T %"

SQL statement executed: select c. ID C_ID, c. NAME, c. AGE, o. ID O_ID, o. ORDER_NUMBER, o. CUSTOMER_IDfrom CUSTOMER c inner join ORDERS o on c. ID = o. CUSTOMER_ID where (c. NAME like't % ')

The query result is:

C_ID
NAME
AGE
O_ID
ORDER_NUMBER
CUSTOMER_ID
 
1
Tom
21
1
Tom_order001
1
 
1
Tom
21
2
Tom_order002
1
 
1
Tom
21
3
Tom_order003
1
 

 

 

Fifth, urgent links
Hql: from Customer cinner join fetch c. orders o where c. name like't %'

Executed SQL: select c. ID C_ID, c. NAME, c. AGE, o. ID O_ID, o. ORDER_NUMBER, o. CUSTOMER_IDfrom CUSTOMER c inner join ORDERS o on c. ID = o. CUSTOMER_ID where (c. NAME like't % ')

The query result is:

C_ID
NAME
AGE
O_ID
ORDER_NUMBER
CUSTOMER_ID
 
1
Tom
21
1
Tom_order001
1
 
1
Tom
21
2
Tom_order002
1
 
1
Tom
21
3
Tom_order003
1
 

 

 

Sixth, right outer link
 

Hql: from Customer c rightjoin fetch c. orders o where c. name like't %'

Executed SQL: select c. ID C_ID, c. NAME, c. AGE, o. ID O_ID, o. ORDER_NUMBER, o. CUSTOMER_IDfrom CUSTOMER c right outer join ORDERS o on c. ID = o. CUSTOMER_ID where (c. NAME like't % ')

The query result is:

C_ID
NAME
AGE
O_ID
ORDER_NUMBER
CUSTOMER_ID
 
1
Tom
21
1
Tom_order001
1
 
1
Tom
21
2
Tom_order002
1
 
1
Tom
21
3
Tom_order003
1
 

 

 

 

The summary is shown in the following table:

Connection Method
Corresponding SQL query
Search policy for the Orders set
Query Result content
 
No connection
Query a single customer table
Delayed retrieval Policy
The set contains the elements of the customer. There are no repeated elements in the set. The order object corresponding to the customer is not initialized.
 
Urgent left Outer Join
Left outer join query customer and order
Urgent left Outer Join search policy
The set contains the elements of the customer. The set may have re-reading elements. The order set attribute of the customer object is initialized.
 
Left Outer Join
Left outer join query customer and order
Delayed retrieval Policy
The collection contains elements of the object data type. Each object array contains a pair of customer objects and order objects. Different object arrays may reference the same customer object repeatedly; the order set attribute of the customer object is not initialized.
 
Internal Connection
Query the customer table and order table through inner join
Delayed retrieval Policy
The collection contains elements of the object data type. Each object array contains a pair of customer objects and order objects. Different object arrays may reference the same customer object repeatedly; the order set attribute of the customer object is not initialized.
 
Urgent internal connection
Internal Connection query customer and order
Urgent link search policy
The set contains elements of the customer type. duplicate elements may exist in the set, and the order set attribute of the customer object is initialized.
 
Outer right link
The outer right link queries the customer table and order
Delayed retrieval Policy
Set contains elements of the object array type. Each object contains a pair of customer and order. Different object arrays may reference the same customer object. The order set attribute of the customer object is not initialized.
 

 

 

Related Article

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.