There are two differences between load and get. First, let's talk about the first delayed loading.
Load is true, get is false
That is to say, 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 talk about how the get method works.
Because hibernate stipulates that the get method cannot use delayed loading, it is different from the load method.
Tfaq tfag2 = (tfaq) sess. Get (tfaq. Class, 300 );
When creating this statement, let's take a look at what hibernate has done.
1. The get method first queries the session cache (the session cache is the first-level cache of hibernate. You should be clear about this concept)
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. We didn't get it from the database. We call it a proxy object. If we get it from the database, we return this object. We call it an object. This object actually exists)
I sum up the difference between the two words
The get method first queries the session cache. If it does not, it queries the second-level cache and the database. Instead, the load method first queries the session cache when it is created. If it does not, a proxy is created, the second-level cache and database are queried only when data is used. (1) If you want to load an object and use its attributes, use the get method, data in the object when the get method is used (2) If you want to add, delete, modify, and query an object, use the load method to improve performance. You can use a proxy object, this eliminates the chance of interacting with the database. It only interacts with the database when the attributes of the object are actually used.