The difference between load method and get method in Hibernate _java

Source: Internet
Author: User
Tags throw exception

The main difference is whether to delay loading.

Load method
The database is not accessed immediately, and the Load method returns an uninitialized proxy object when the record you are trying to load does not exist.

Get method

Always access the database immediately. Returns null directly when the record you are trying to load does not exist

In Hibernate, 2 methods are found to be identical. After the inspection found

There are two very similar method get () and load () in Hibernate, they can read the data from the database by the specified entity class and ID, and return the corresponding instance, but hibernate will not do two identical methods, the difference between them is:

Get is a direct query to the database, if you do not find the return of NULL, load will first be loaded from memory, if not previously loaded or query out before starting the query database.

The fundamental difference between the Get method and the Load method in Hibernate is that if you use the Load method, the object that the ID corresponds to (the database record) must exist in the database, so it can be used with ease, and it can use the agent to delay loading the object hibernate. 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 the Get method, hibernate will confirm that the ID corresponds to the data, first look in the session cache, then look in the level two cache, not the database, and return NULL in the database.

2.get method First query session cache, no words query level two cache, the last query database; instead, the Load method first queries the session cache when it is created, does not create the agent, and queries the level two cache and database when the data is actually used.

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, if the use of the process found that the problem can only throw exception; Hibernate must obtain the real data, otherwise return NULL.

1. If no records are found that match the criteria, the Get () method returns NULL. and load () will report objectnotfoundecception.
2. The load () method can return an instance of the proxy class for an entity, and get () always returns an entity class.
3. The load () method makes full use of the existing data of level two cache and internal cache, and the Get () method only looks in the internal cache, and if no corresponding data is found to skip the level two cache, call SQL to complete the lookup directly.

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.