Differences between the load () and get () Methods of sessions in hibernate

Source: Internet
Author: User

A:

Hibernate has two extremely similar methods: Get () and load (). Both of them can read data from the database through the specified object class and ID, and return the corresponding instance, however, Hibernate won't adopt two identical methods. The difference between them is:

1. If no matching record is found, the get () method returns NULL. The load () method returns objectnotfoundecception.

2. The load () method can return the proxy class instance of the object, while get () always returns only the object class.

3. the load () method can fully utilize the existing data in the second-level cache and the internal cache, while the get () method only searches in the internal cache. If no data is found, the second-level cache is skipped, directly call SQL to complete the search.

B:

Hehe didn't talk about the root point. The fundamental difference between the get method and the load method in Hibernate is that if you use the load method, Hibernate considers the object corresponding to this ID (Database record) it must exist in the database, so it can be safely used. It can safely use a proxy to delay loading the object. The database is queried only when other attribute data in the object is used. However, if the record does not exist in the database, no exception can be thrown, the load method throwing exception refers to throwing an exception when the data of this object does not exist in the database, rather than when the object is created. Since the session cache is a very cheap resource for hibernate, during load, we will first check the session cache to see if the object corresponding to this ID exists. If it does not exist, we will create a proxy. So if you know that this ID must have a corresponding record in the database, you can use the load method to implement delayed loading. 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.

For, although many books have said this: "Get () always returns only entity classes", this is actually incorrect, 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, however, Entity Data has been loaded.

3. Nonsense. As mentioned above, the get method first queries the session cache. If not, It queries the second-level cache and the database. Instead, when the load method is created, it first queries the session cache and creates a proxy if not, the second-level cache and database are queried only when data is actually used.

In short, there is a fundamental difference between get and load. In a word, Hibernate considers the load method to exist in the database and can safely use the agent to delay loading, if a problem is found during use, only an exception can be thrown. For the get method, Hibernate must obtain the actual data; otherwise, null is returned.

C:

There are a lot of discussions on the load and get methods on the Internet. I made a small experiment to clarify the working principles of the load and get methods. First, there is nothing to say about the get method. It is to hit the database when the session executes this function, while the load method is troublesome. The specific execution process is as follows:
JavaCode
Session session = getsessionfactory (). opensession ();
Transaction TR = session. begintransaction ();
// Student stu2 = (student) Session. Get (student. Class, new INTEGER (5 ));
// If (session. Contains (stu2) system. Out. println ("stu2 in the session ");
Student Stu = (student) Session. Load (student. Class, new INTEGER (5 ));
Stu. getaddress ();
Tr. Commit ();
Session. Close ();

(1) Check whether cached persistent objects exist in the persistent context of the session, if yes, the persistent object is directly returned as the stu object. If no, a proxy object needs to be created. The proxy object is not considered as a pojo. The initialized attribute of the proxy object is false, the target attribute is null. (2) when accessing the properties of the obtained proxy object, for example, when Stu. getarress () is executed, because the persistent context does not contain this persistent object, the hit database is used. (3) In the hit database, if the corresponding record of the object is found in the database, assign the obtained object to the target attribute of the proxy object and change the initialized attribute to true; if the record corresponding to this object cannot be found in the database, org. hibernate. objectnotfoundexception exception. The get method hit database is used for each execution. If there is no corresponding record, null is returned.

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.