The Get () and load () methods provided by the session interface in Hibernate are used to obtain an entity object, with some differences in usage and query performance.
Get
The session interface provides 4 overloaded get methods that obtain entity objects through persistent class + primary key and full class name + primary key and lock options.
public object get (class Clazz, Serializable ID);p ublic object Get (class Clazz, Serializable ID, lockoptions lockoptions);p Ublic object Get (String entityname, Serializable ID);p ublic object Get (String entityname, Serializable ID, lockoptions Loc Koptions);
Load
Load is basically the same way as get calls, with an overloaded method: Use an instance of an empty persisted class + primary key to get the entity object.
public void Load (Object object, Serializable ID);
The difference between get and load
Comment out the print statement separately, that is, just get the object, not the object used. You can see that the GET method issued an SQL statement, and the load method does not emit an SQL statement.
The ①get method returns a persisted object immediately after the call is issued to the database, regardless of the cache, and the Load method returns a proxy object that only holds the ID of the entity object, after the call. The SQL statement is not emitted until the object's non-primary key property is used.
② The Get method returns the Null,load method throws an exception when querying data that does not exist in the database :org.hibernate.ObjectNotFoundException.
About lazy loading and caching
Deferred load is enabled by default after Hibernate3: Lazy= "True", the Get method does not use a lazy load mechanism, first finds the session cache, then looks for a level two cache, and then finds the database. The Load method first looks for the session cache, then looks for a level two cache, returns the proxy object if it is not found, and defers the SQL statement load object when it is actually using the object's non-progressive properties. If the lazy= "false" is set, the load returns the proxy object and the SQL statement is issued, and if no qualifying record is found, the objectnotfoundexception exception is thrown.
The Get () and load () method differences for Hibernate session