The difference between get and load,find in Hibernate.

Source: Internet
Author: User

Get and load mode is a record based on ID
The difference between get and load is detailed below, because sometimes it is added in order to compare.

1. Compare the results from the return:
A Org.hibernate.ObjectNotFoundException exception is thrown if the load method is not retrieved
The Get method returns null if it is not retrieved

2. Comparison from the search execution mechanism:
Both the Get method and the Find method are retrieved directly from the database
The execution of the load method is more complex
First find out if there is a cache in the persistent context of the session, and if so, return directly
If not, determine if it is lazy, if you do not directly access the database retrieval, the records returned, the exception is not found
If it is lazy, you need to establish a proxy object, the initialized property of the object is False,target property is null
When accessing the properties of the obtained proxy object, the database is retrieved, and if a record is found, the object of the record is copied to the target of the proxy object, and the exception is thrown if the initialized=true is not found.

3. The fundamental difference statement
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. The Load method thrown exception refers to throwing exceptions when the data is not present in the database when the object is being used, rather than when creating the object (Note: This is because of "lazy loading" in mischief).

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.

For the load and get method return type: Although many books say, "Get () always returns only entity class", but in fact this is not true, the Get method if the ID corresponding to the object found in the session cache, if just before the object is proxied, If used by the Load method, or delayed by other associated objects, then the original proxy object is returned, not the entity class object, if the proxy object has not yet 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, However, the proxy object is returned, 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.

4. Brief summary

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