The difference between hibernate's load and get methods

Source: Internet
Author: User

Let's talk about this time. Hibernate3.2 Session The difference between the get and load methods when loading data, I summarize the following:

1. for the Get method, hibernate confirms that the data for the ID exists, first in the session cache, and then in the two cache, and returns NULL if no query is in the database database. This is relatively simple, and there is not much controversy. The main point to note is that in this version of the Get method will also find the level two cache!

2. the Load method loads the entity object, based on the configuration of the lazy attribute at the class level on the mapping file (true by default), which is discussed in some cases:

(1) If true, first look in the session cache to see if the ID corresponds to the existence of the object, does not exist then use lazy loading, return the entity's proxy class object (the proxy class is a subclass of the entity class, generated dynamically by Cglib). Wait until the specific use of the object (in addition to the OID) to query the level two cache and database, if you still do not find a qualifying record, will throw a objectnotfoundexception.

(2) If False, it is the same as the Get method lookup order, but eventually if no qualifying records are found, a objectnotfoundexceptionwill be thrown.

Here there are two important differences between get and load:

1. If a qualifying record cannot be found, the Get method returns NULL, and the load method throws a objectnotfoundexception.

2. The Load method returns an instance of a proxy class that does not have the Entity data loaded, and the Get method always returns an object with Entity data. (For load and get method return types: "Get methods always return entity classes" is not actually correct, if the Get method finds the object corresponding to the ID in the session cache, if the object is previously proxied, if it was used by the Load method, or delayed by other associated objects, then the original proxy object is returned, not the entity class object, if the proxy object has not loaded the Entity data (that is, other than the ID of the property data), then it will query the level two cache or the database to load the data, but the return is the proxy object, Only the Entity data has been loaded. )

In short, for get and load the fundamental difference, in a word, hibernate for the load method that the data in the database must exist, can be assured that the use of agents to delay loading, if the use of the problem found in the process, can only throw exceptions, and for the Get method, Hibernate must obtain the actual data, otherwise null is returned.

Finally, we analyze why there are so many articles on the internet about the difference between the less accurate! The first may be the version problem, hibernate version is different, the operating mechanism is not the same; Dong told everyone: I need to organize and think it! Practice is the only standard to test truth, I was a liberal arts in high school, with this word to tell you the importance of truth! The knowledge that is obtained is true and effective.

The difference between hibernate's load and get methods

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.