Talking about Hibernate n + 1, talking about hibernate

Source: Internet
Author: User

Talking about Hibernate n + 1, talking about hibernate

The Session cache stores the correlated object graphs. By default, when Hibernate loads the Customer object from the database, it loads all associated Order objects at the same time. Take the Customer and Order classes as an example. Assume that the foreign key CUSTOMER_ID of the ORDERS table can be null.

BelowFind () of Session ()The method is used to retrieve all Customer objects in the database:

List customerLists=session.find("from Customer as c");

Runfind()Hibernate first queries all records in the MERs table, and then queries records with reference relationships in the ORDERS table based on the ID of each record. Hibernate will execute the following select statement in sequence:

Select * from MERs MERS;
Select * from ORDERS where CUSTOMER_ID = 1;
Select * from ORDERS where CUSTOMER_ID = 2;
Select * from ORDERS where CUSTOMER_ID = 3;
Select * from ORDERS where CUSTOMER_ID = 4;

Using the preceding five select statements, Hibernate finally loads four Customer objects and five Order objects, forming an associated object diagram in the memory.

Hibernate uses the default immediate search policy when retrieving the Order object associated with the Customer.There are two shortcomings in this search policy:

(1) The number of select statements is too large. Frequent database access is required, which affects the retrieval performance. To query n Customer objects, you must execute n + 1 select query statement. This is the classic n + 1 select query problem. This search policy does not use the SQL connection query function. For example, the preceding five select statements can be completed using one of the following select statements:

Select * from MERs left outer join ORDERS
On CUSTOMERS. ID = ORDERS. CUSTOMER_ID

The preceding select statement uses the SQL left outer join query function to query all records of the MERs table and matching records of the ORDERS table in a select statement.

(2) When the application logic only needs to access the Customer object instead of the Order object, loading the Order object is totally redundant, these redundant Order objects waste a lot of memory space.

To solve the above problems, Hibernate provides two other Retrieval Strategies: The latency retrieval policy and the urgent left Outer Join retrieval policy. The latency search policy can avoid unnecessary loading of the associated objects that the application does not need to access. The urgent left Outer Join search policy makes full use of the SQL outer join query function, which can reduce the number of select statements.

For database access, performance issues must still be taken into account. After a one-to-many relationship is set, the legendary n + 1 issue may occur during queries.

1) if n objects are found on the first side of a pair, n objects need to be retrieved from the set associated with the n objects, so the original SQL query is changed to n + 1

(2) If multiple objects are queried for multiple objects and m objects are obtained, the one-party object corresponding to m objects is also taken out and changed to m + 1.

How to solve the n + 1 problem?

1) lazy = true. The default value of hibernate3 is lazy = true. When lazy = true, the associated object is not queried immediately. Only when the associated object is required (access its properties, non-id field) the query will only take place.

2) The second-level cache, in which objects are updated, deleted, and added is much less than the query, the second-level cache application will not be afraid of the n + 1 problem, because even if the first query is slow, the direct cache hit is also very fast.
Different solutions have different ideas, but the second one uses n + 1.

3) You can also setfetch=join(annotation : @ManyToOne() @Fetch(FetchMode.JOIN))

Summary

The above is all about Hibernate n + 1 in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.