The difference between load and get methods in Hibernate

  • 2020-04-01 04:33:56
  • OfStack

The main difference is whether to lazily load.

The load method
The database is not immediately accessed, and the load method returns an uninitialized proxy object when the record being loaded does not exist.

The get method

Always access the database immediately. When the record being loaded does not exist, null is returned directly

The two methods are found to be very similar in hibernate. Look up and find

Hibernate has two very similar methods get() and load(), both of which can read data from the database and return the corresponding instance through the specified entity class and ID, but Hibernate will not do the two exactly the same method, the difference between them is that:

Get is a direct query to the database. If it cannot be found, it will return null. Load will be loaded from memory first.

The fundamental difference between the get method and the load method in hibernate is that if you use the load method, hibernate assumes that the object (database record) corresponding to the id must exist in the database, so it can be safely used. It can safely use the proxy to lazily load the object. The database is only queried when the data of other attributes in the object is used, but in case the record does not exist in the database, there is no way but to throw an exception. The load method throwing exception refers to throwing an exception when the data of the object is used and the data does not exist in the database, but not when the object is created. Since the cache in session is a fairly inexpensive resource for hibernate, the session cache is checked to see if the object corresponding to the id exists in load, and the proxy is created if it does not. So if you know that the id must have a record in the database then you can use the load method to do lazy loading. For the get method, hibernate will verify whether the data corresponding to this id exists, first in the session cache, and then in the secondary cache, not yet check the database, the database does not return null.

2. The get method first queries the session cache, if there is no second level cache, and finally queries the database; Instead, the session cache is first queried when the load method is created, the proxy is not created, and the second level cache and database are queried when the data is actually used.

In short, for the fundamental difference between get and load, in a word, hibernate thinks that the data must exist in the database for the load method, you can rest assured to use the proxy to delay the load, if problems are found in the use process, can only throw exceptions; For the get method, hibernate must get the real data or return null.

1. If a qualified record cannot be found, the get() method returns null. And load() will report an ObjectNotFoundEcception.
The load() method returns an instance of the proxy class of the entity, whereas get() always returns only the entity class.
3. The load() method can make full use of the existing data in the second level cache and internal cache, while the get() method can only search in the internal cache. If the corresponding data is not found, it will skip the second level cache and directly call SQL to complete the search.


Related articles: