The difference between load and get in Hibernate

Source: Internet
Author: User

The Session.load/get method in Hibernate can read records from the database based on the specified entity class and ID and return the entity object corresponding to it. The difference is:

The 1.get () method returns the entity class directly, and the load () method can return the proxy class instance of the entity.

2.hibernate load is a delay mechanism (when the Lazy property is true) and get does not use a delay mechanism (the take reads the library immediately)

3. Unable to find a qualifying data get method returns null

Load will quote Objectnotfoundexcepion

4.get support for polymorphic queries, load only supports polymorphic queries in the case of Lazy=false

The so-called polymorphic query, that is, can clearly distinguish what type of object loaded, load agent mechanism can not support

Deferred loading: Hibernate as far as possible delay to send SQL to the database, it has a buffer itself, first put the SQL inside, and finally sent together to reduce network overhead and database overhead.

Load method Principle:

When the object. Hbm.xml profile element's Lazy property is set to True, the call to the load () method returns the proxy class instance of the persisted object, at which point the proxy class instance is a class generated dynamically by the runtime, and the proxy class instance includes all the properties and methods of the original target object. The property of the proxy class instance has a null value, except that the ID is not NULL, and the log does not hibernate the SQL output, which means that the query operation is not performed, and when the proxy class instance obtains the property value through the GetXXX () method, Hiberante to actually perform database query operations.

Attention:

01.StudentEh s=(StudentEh)session.load(StudentEh.class,10);
02.
03.System.out.println(s.getId());

These two statements also do not produce SQL statements, because Session.load will store a map object in the Hibernate cache, the key of which is the value of the ID, but when you GetID (), it takes the key value of the map from the first cache. Instead of executing database queries.

When an object. Hbm.xml profile element's Lazy property is set to False, the call load () method executes the database immediately and returns the entity class directly, and does not return a proxy class. When the Get () method is invoked, the entity class is returned directly regardless of the value of the lazy.

Get method Principle:

Get method, hibernate will confirm that the ID corresponds to the existence of the data, first in the session cache, and then look up in the level two cache, not to the database, the database does not return NULL.

If the Get method finds an object in the session cache that corresponds to the ID, if the object is previously represented by the Load method, or if it has been deferred by another associated object, then the original proxy object, not the entity class object, is returned. If the proxy object has not yet loaded the Entity data (that is, other property data other than the ID), it queries the level two cache or database to load the data, but returns the proxy object, except that the Entity data has already been loaded.

Summary description:

If you use the Load method, hibernate that the object that the ID corresponds to (the database record) must exist in the database, so it is safe to use the agent to delay loading the object. Query the database when you use other property data in the object. But in the event that the record does not exist in the database, there is no way to throw the exception, and the said Load method throw exception is when the object's data is used and the exception is thrown when the data is not present in the database, rather than when the object is created. Because the cache in session is a fairly inexpensive resource for hibernate, it is necessary to check the sessions cache first to see if the object in the ID is present or not, and then create the proxy. So if you know that the ID must have a corresponding record in the database, you can use the Load method to implement deferred loading.

For a Get method, hibernate must obtain the real data, otherwise it returns NULL.

The problems that you are experiencing in your development process:

We use the MyEclipse Hibernate tool to generate the DAO class from the database, Its FindByID method is to use the Session.get () method, which is to obtain the Pojo object immediately, if the load method, after the completion of the load if the session is closed, then when the next use of the Pojo object will report the session has been closed error.

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.