Difference between get and load in Hibernate

Source: Internet
Author: User
For the get method, hibernate checks whether the data corresponding to this id exists. First, it searches for the data in the session cache, and then searches for the data in the second-level cache. If no data exists, it queries the database.

For the get method, hibernate checks whether the data corresponding to this id exists. First, it searches for the data in the session cache, and then searches for the data in the second-level cache. If no data exists, it queries the database.

1. for the get method, hibernate checks whether the data corresponding to this id exists. First, it searches in the session cache and then in the second-level cache. If no data exists, it queries the database, if the database does not exist, null is returned. This is relatively simple and there is no much controversy. The main note is that in this version, the get method will also find the second-level cache!

2. When loading an object using the load method, the following situations are discussed based on the configuration of the Class-level lazy attribute in the ing file (the default value is true:

(1) If the value is true, first query the Session cache to check whether the object corresponding to this id exists. If the object does not exist, use delayed loading ,, returns the proxy object of an object (this proxy class is a subclass of the object class, which is dynamically generated by CGLIB ). When this object is used (except for obtaining OID), the second-level cache and database will be queried. If no matching record is found, an ObjectNotFoundException will be thrown.

(2) If the value is false, the query sequence is the same as that of the get method. However, if no matching record is found, an ObjectNotFoundException will be thrown.

Here, get and load have two important differences:

If no matching record is found, the get method returns null, and the load method throws an ObjectNotFoundException.
The load method returns a proxy instance that does not load object data, and the get method returns objects with Object Data forever. (For the return type of load and get Methods: many books say: "The get method always returns only the object class", which is not true, if the get method finds the object corresponding to this id in the session cache, if the object is replaced by a proxy, for example, it is used by the load method, or delayed loading by other associated objects, the returned result is the original proxy object instead of the Object Class Object, if the proxy object has not loaded Entity Data (that is, attribute data other than id), it will query the second-level cache or database to load data, but the returned result is still a proxy object, only Entity Data has been loaded .)

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.