Hibernate Data Loading--session.get/load

Source: Internet
Author: User

Session. Both the 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 that 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 loading an entity object: 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 then searched 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 the corresponding record is not found in the query, the information for this query is recorded in "Nonexists" and returned null. Create the corresponding data object based on the mapping configuration and the resultset obtained by the Select SQL. The data object is included in the current session Entity Management container (first-level caching). Executes the Interceptor.onload method (if there is a corresponding interceptor). The data object is included in the level two cache. If the data object implements the lifecycle interface, the OnLoad method of the data object is invoked. Returns the 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.