The fundamental difference between the Get method and the Load method in Hibernate

Source: Internet
Author: User
Tags throw exception

If you use the Load method, Hibernate believes that the object (database record) corresponding to the ID must exist in the database, so it can be used with confidence, and it can safely use the proxy to delay loading the object. The database is queried for other property data in the object, but in case the record does not exist in the database, there is no way to throw an exception, and the Load method throw exception means that when the data for that object is used, the exception is thrown when the data in the database is not present, rather than when the object is created. Because the cache in the session is a fairly inexpensive resource for Hibernate, the session cache is checked at load to see if the object exists for that ID, and the proxy is created if it does not exist. So if you know that the ID must have a corresponding record in the database, you can use the Load method to implement lazy loading.
For the Get method, Hibernate confirms the existence of the data for that ID, first in the session cache, then in the level two cache, and not in the database, which returns null in the database.

Although it is said in many books, "get () will always return only entity class", but in fact this is not correct, if the Get method is found in the session cache object corresponding to the ID, if the object is just before it was proxied, such as used by the Load method, or by other associated objects deferred loading , the original proxy object is returned, not the entity class object, and if the proxy object has not yet loaded the Entity data (that is, the attribute data other than the ID), it queries the level two cache or the database to load the data, but returns the proxy object, except that the entity data has been loaded.

The Get method first queries the session cache, does not query the level two cache, and finally queries the database; Instead, the Load method first queries the session cache, does not create the proxy, and queries the level two cache and database when the data is actually used.

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 be sure to get the actual data, otherwise return null

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.