Difference between hibernate get and Load

Source: Internet
Author: User

The get and load methods are used to obtain a record based on the ID.
The difference between get and load is described in detail below, because sometimes find will be added for comparison.

 

1. Compare the returned results:
The Org. hibernate. objectnotfoundexception exception will be thrown if the load method cannot be retrieved.
If the get method cannot be retrieved, null is returned.

 

2. comparison from the retrieval execution mechanism: the get method and the find method are both retrieved directly from the database, while the execution of the load method is complicated. First, check whether the session persistent context has a cache, if yes, the system returns the result directly. If no result is returned, the system determines whether the result is lazy. If the result is not directly accessed to the database for retrieval, the system queries the record and returns the result. If the result is lazy, a proxy object needs to be created, the initialized attribute of the object is false, and the target attribute is null. when accessing the properties of the obtained proxy object, the database is retrieved. If a record is found, the object of the record is copied to the target of the proxy object, and initialized = true. If it cannot be found, an exception is thrown.

 

3. Basic Differences
If you use the load method, Hibernate deems that the object corresponding to this ID (Database record) exists 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 creating this object (note: this is due to the "delayed loading ).

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.

 

4. Summary

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.