Hibernate loads persistent objects in two ways--get, load

Source: Internet
Author: User
Tags throw exception

A. Get vs. Load control

The Get and load methods in Hibernate are two ways to get persisted objects by ID. But in the actual use of the process will always confuse the two, do not know when to use get good, when to use the Load method more efficient. Here's a look at the difference between get and load, and sometimes find is added to the control.

1. From the return result: Org.hibernate.ObjectNotFoundException exception is thrown when load mode is not retrieved
The Get method returns null if it is not retrieved

2. From the search operation mechanism, compare:

Both the Get method and the Find method are retrieved directly from the database and the Load method is more complex to first find out if there is a cache in the persistent context of the session. Suppose there is a direct return assumption that there is no inference whether it is lazy. Assume that you are not directly visiting the database to retrieve the records returned. The throw exception is not found. The assumption is that lazy needs to establish a proxy object. The initialized property of the object is false. The target property is null when visiting the properties of the obtained proxy object, the database is retrieved, assuming that the record is found to copy the object of the record to the target of the proxy object. And will initialized=true, assuming that the exception is thrown if it is not found.

3. The fundamental difference is that if you use the Load method, hibernate feels that the corresponding object (database record) of the ID must exist in the database. So it can be used with ease, and it can safely use the proxy to delay loading the object. The database is queried for other attribute data in the object, but in case the record does not exist in the database, there is no way to throw an exception.

The Load method thrown exception refers to throwing exceptions when the data in the database is used, rather than when the object is created.

Because the cache in session is a fairly inexpensive resource for hibernate. So the session cache is checked at load to see if the corresponding object exists, and the proxy is created if it does not exist.

So suppose you know that the ID must have a corresponding record in the database to be able to use the Load method to implement deferred loading.

For the Get method, hibernate confirms that the corresponding data for the ID exists, first in the session cache, and then in the level two cache. The database has not been checked, and NULL is not returned in the database.

For the load and get method return types: Although many books say, "Get () always returns only entity classes." But in fact this is not true, the Get method assumes that the ID corresponding object is found in the session cache, assuming that the object is just before it has been proxied. If it is used by the Load method or is delayed by other associated objects, then the original proxy object is returned, not the entity class object, assuming that the proxy object has not loaded the Entity data (that is, other than the ID of the property data), then it will query the level two cache or database to load the data, but Returns the proxy object. Only the Entity data is already loaded.

The Get method queries the session cache first, without querying a level two cache. Finally, query the database. Instead, when the load method is created, the session cache is queried first, no proxies are created, and the data is actually used to query level two caches and databases.


Two. Analysis of usage

As to what is the case when using get load. I feel that the most essential thing is to see the persistent objects and the amount of data to load.

There is no significant difference between using get and load when the object to be loaded does not have a complex connection to other objects, and there is a complex association between the object to be loaded and the other object. It is more efficient to use the Load method than the Get method when the number of associations between objects is relatively large.

For example, classes and students two persistent object entities, when the class information is also loaded into the corresponding class of all student information. But suppose that in the program we now need the knowledge of class information without the need for student information. In such cases, the Get method waits until the class information is loaded with the student's information, and the Load method first loads the class information and saves the relationship between the class and the student until the actual use of the student's information is loaded. So in the case of complex relationships and large amounts of data, the load mode should have obvious advantages.

three. Brief summary

In short, the fundamental difference between get and load. In a word, hibernate for the load method that the data must exist in the database, can be assured that the use of agents to delay loading, assuming that the use of the problem found in the process, only to throw exceptions, and for the Get method, hibernate must obtain the actual data, otherwise return NULL. In the actual use of the process to assume that the use of which, directly using the Load method can be.

Hibernate loads persistent objects in two ways--get, load

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.