Differences between get and load methods in hibernate

Source: Internet
Author: User

The fundamental difference between the get method and the load method in Hibernate is that if you use the load method, Hibernate recognizes the object corresponding to this ID (Database record) as existent in the database, so it can be safely used, and 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 exception thrown by the load method means that when the data of this object is used, the exception is thrown when the data in the database does not exist, 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 the return types of load and get Methods: 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.

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 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.

 

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.