Difference between get and load in hibernate

Source: Internet
Author: User

From: http://www.cnblogs.com/gxinliug/archive/2011/04/26/2029788.html

There are two differences between load and get. First, let's talk about the first delayed loading.
Load is true, get is false
The idea is that load uses the delayed loading method instead of get. hibernate thinks that this object must exist in the Database since this method supports delayed loading, after you declare tfaq tfag2 = (tfaq) sess. load (tfaq. class, 300); in this sentence, Hibernate does one thing.
1. query session cache
2. Create a proxy if this object is not in the cache
A proxy is created for delayed loading.
OK, so far, this statement has been done and does not go to the database for interactive query.
When you use this object, such as tfag2.gettfrtitle () or get method
At this time, Hibernate queries the second-level cache and database.If the database does not have this data, an exception is thrown.
The whole load method call ends load. Nothing magical. This is what he did.

the load method is finished. I'm going to explain how the get method works.
the load method is different because the get method does not support delayed loading.
tfaq tfag2 = (tfaq) sess. get (tfaq. class, 300);
when creating this statement, let's see what hibernate has done
1. the get method first queries the session cache (the session cache is the first-level cache concept of hibernate)
2. 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, otherwise, the returned result is the original proxy object instead of the Object Class Object.
3. 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, however, Entity Data has been loaded.
(this proxy is actually an empty object that is not queried by the database. We call it a proxy object, if this object is returned after a database query, we call it an object that actually exists)

Summary of the two differences in one sentence
The get method first queries the session cache, if no, the second-level cache is queried and the database is finally queried. Instead, the session cache is queried when the load method is created, and a proxy is created if no session cache is found, the second-level cache and database are queried only when data is actually used

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.