The difference of Session.get ()/load () in Hibernate

Source: Internet
Author: User

The Session.load/get method can read records from the database based on the specified entity class and ID and return the entity object corresponding to it. The difference is:

If a record that matches a condition is not found, the Get method returns NULL, and the load method throws a objectnotfoundexception.
The Load method returns an instance of the proxy class for an entity, and the Get method always returns the entity class directly.
The Load method can take full advantage of the existing data in the internal cache and the level two cache, while the Get method only makes data lookups in the internal cache, and if no corresponding data is found, it will bypass the level two cache and directly invoke SQL to complete the data reading.

The procedure that the


session passes when the entity object is loaded:

First, the level two cache is maintained in Hibernate. The first level cache is maintained by the session instance, which maintains the data of all associated entities currently in session, also known as internal caching. The second level cache exists at the sessionfactory level and is shared by all current session instances constructed by this sessionfactory. For performance reasons, avoid unnecessary database access, the session in the database query function before calling, will first query in the cache. First, in the first level cache, the entity type and id are searched, and if the first level cache lookup hits and the data status is valid, it is returned directly.
, the session is found in the current nonexists record, and null if the same query condition exists in the nonexists record. "Nonexists" records a query condition (equivalent to a list of query blacklists) in which the current session instance failed to query for valid data in all previous query operations. As a result, if an invalid query condition in the session repeats itself, the decision can be made quickly to get the best performance.
for the Load method, if no valid data is found in the internal cache, the second-level cache is queried and returned if a second-level cache hit.
If no valid data is found in the cache, the database query operation (Select SQL) is initiated, and if no corresponding record is found in the query, the information for this query is recorded in "Nonexists" and returned null. The
creates the corresponding data object, based on the resultset obtained by the mapping configuration and select SQL. The
incorporates its data objects into the current session Entity Management container (first-level caching). The
Executes the Interceptor.onload method (if there is a corresponding interceptor). The
includes data objects in level two caching.
The OnLoad method of the data object is invoked if the data object implements the lifecycle interface. The
returns a data object.

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.