The main difference is whether it is delayed loading.
The load method does not access the database immediately, and when the record attempted to load does not exist, the load method returns an uninitialized proxy object.
get method
Always access the database immediately. When the record attempted to load does not exist, return null directly
There are two methods found in hibernate that are very similar. After reviewing it, I found
There are two very similar methods in Hibernate, get() and load(). They can both read data from the database through the specified entity class and ID and return the corresponding instance. However, Hibernate will not do two exactly the same methods. The difference between them is:
get is to query the database directly. If it cannot be found, it will return null. Load will first be loaded from memory. If it has not been loaded or queryed before, then querying the database.
The fundamental difference between the get method and the load method in hibernate is that 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 use the proxy to delay loading the object. The database is only queried when other attribute data in the object is used, but if the record does not exist in the database, there is no way, and you can only throw exceptions. The load method throws exceptions means that when using the data of the object, the exceptions are thrown when the data does not exist in the database, rather than when creating this object. Since the cache in the session is a very cheap resource for hibernate, when loading, we will first check the session cache to see if the object corresponding to the id exists. If it does not exist, a proxy will be created. So if you know that the id must have a corresponding record in the database, you can use the load method to achieve lazy loading. For the get method, hibernate will confirm whether the data corresponding to the id exists. First, look up in the session cache, and then look up in the secondary cache. If it has not been done yet, look up the database, and if it has not been done in the database, return null.
2. The get method first querys the session cache, if not, querys the secondary cache, and finally querys the database; on the contrary, when the load method is created, the session cache is first query, and if there is no agent is created, the secondary cache and database are only queryed when the data is actually used.
In short, for the fundamental difference between get and load, in a word, hibernate believes that the data must exist in the database for load method, and can use the proxy to delay loading with confidence. If a problem is found during use, it can only throw exceptions; for the get method, hibernate must obtain the real data, otherwise it will return null.
1. If no record that meets the criteria is found, the get() method will return null. And load() will report an ObjectNotFoundEcception.
2. The load() method can return an entity's proxy class instance, while get() will always return only the entity class.
3. The load() method can make full use of the existing data in the secondary cache and the internal cache, while the get() method only searches in the internal cache. If no corresponding data is found, the secondary cache will be skipped and SQL will be directly called to complete the search.