The Get () and load () method differences for Hibernate session

Source: Internet
Author: User

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

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.