Differences between get and load methods in hibernate

Source: Internet
Author: User

Let's talk about it this time.Hibernate3.2The difference between get and load methods when loading data in sessions. In fact, there are a lot of discussions on the Internet, which can be vague or difficult to comprehend, making it difficult for many beginners to learn, now I will explain to you:


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:

  1. If no matching record is found, the get method returns NULL, and the load method throws an objectnotfoundexception.
  2. 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 .)


In short, for getAnd loadThe fundamental difference, in a word, HibernateFor LoadThe method assumes that the data exists in the database, so you can use the proxy to delay loading. If a problem is found during use, you can only throw an exception.Method, HibernateBe sure to obtain the real data; otherwise, null is returned..


Finally, let's analyze why so many articles on the Internet about the differences between the two are not accurate! The first issue may be the version issue. The runtime mechanism is not the same for different hibernate versions. Secondly, many friends share their experience with others, and have not gone through comprehensive code detection; finally, some technical experts are more casual. Therefore, I hope that you will not blindly learn from others' scientific authority in the future. You must combine multiple materials, compare and organize them, and then pass your own practical tests. The obtained knowledge is true and effective.


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.