Hibernate data loading -- session. Get/load

Source: Internet
Author: User

The session. Load/get method can read records from the database based on the specified object class and ID, and return the corresponding object. The difference is:

    1. If no matching record is found, the get method returns NULL, and the load method throws an objectnotfoundexception.
    2. The load method returns the proxy class instance of the object, while the get method returns the object class directly.
    3. The load method can make full use of the existing data in the internal cache and the second-level cache, while the get method only searches for data in the internal cache. If no corresponding data is found, the second-level cache will be crossed, directly call SQL to read data.

When a session loads an object, it goes through the following process:

    1. first, two levels of cache are maintained in hibernate. The first level of cache is maintained by the session instance, which maintains the data of all associated entities of the session, also known as the internal cache. The second-level cache exists at the sessionfactory level and is shared by all session instances constructed by the current sessionfactory. In order to avoid unnecessary database access for performance consideration, session queries in the cache before calling the database query function. First, in the first-level cache, the object type and ID are used for search. If the first-level cache query hits and the data status is valid, the system returns the result directly.
    2. then, the session searches for the current "nonexists" record. If the same query condition exists in the "nonexists" record, null is returned. "Nonexists" records the query conditions of the current session instance in all previous query operations, and the query conditions of valid data cannot be queried (equivalent to a query blacklist list ). In this way, if an invalid query condition reoccurs in the session, you can quickly make a judgment to obtain the best performance.
    3. for the load method, if no valid data is found in the internal cache, the second-level cache is queried. If the second-level cache hits, the second-level cache returns.
    4. if no valid data is found in the cache, select SQL is initiated. If no corresponding record is found after query, the query information is recorded in "nonexists" and null is returned.
    5. Create a Data Object Based on the resultset obtained by the ing configuration and select SQL.
    6. include the data objects in the current Session Object Management container (level-1 cache ).
    7. execute the Interceptor. onload method (if there is a corresponding interceptor ).
    8. include data objects in the second-level cache.
    9. If the data object implements the lifecycle interface, the onload method of the data object is called.
    10. return 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.