What is the difference between the get () and load () methods of a hibernation session with respect to fetching?

What is the difference between get () and load ()? regarding the data selection approach

public static void main(String[] args) { SessionFactory factory= new Configuration().configure().buildSessionFactory(); Session session = factory.openSession(); Transaction tx = null; tx = session.beginTransaction(); System.out.println("1 st time calling load method"); Account acc = (Account)session.load(Account.class, 180); System.out.println("bal"+acc.getBalance()); System.out.println("2nd time calling load method"); Account acc1=(Account)session.load(Account.class, 180); System.out.println("bal"+acc1.getBalance()); System.out.println("1 st time calling get method"); Account acc2= (Account) session.get(Account.class, accId); System.out.println("bal"+acc2.getBalance()); System.out.println("2 st time calling get method"); Account acc2= (Account) session.get(Account.class, accId); System.out.println("bal"+acc2.getBalance()); tx.commit(); session.close(); 

}

I got the following output

 1 st time calling load method Hibernate: /* load com.abcd.Account */ select account0_.ACCOUNTID as ACCOUNTID1_0_, account0_.ACCOUNTTYPE as ACCOUNTT2_1_0_, account0_.CREATIONDATE as CREATION3_1_0_, account0_.BALANCE as BALANCE1_0_ from a.MYACCOUNT account0_ where account0_.ACCOUNTID=? bal3000.0 2nd time calling load method bal3000.0 1 st time calling get method bal3000.0 2 st time calling get method bal3000.0 

The output shows that the get method did not get into the database. It behaves like a load () method. Can anyone tell me this is the correct behavior.

+7
source share
11 answers

As Mishra says, here :

  • By default, hibernate creates proxies at runtime. It downloads objects as a proxy server if the fetch mode is not specified or is not set to false.

  • That, as soon as the object is loaded into the cache, subsequent subsequent calls carry out repeated reading.

  • Although the state of this object changes from constant to disconnected

An entity can be restored in two ways.

load () - returns a proxy object with an identifier.

get () - returns the full object from the database.

for more details click the link

+5
source

In fact, both functions are used to retrieve an object with a different mechanism,

  • Session.load ()

    It will always return a "proxy" (the term Hibernate) without getting into the database. In Hibernate, a proxy is an object with a given identifier value, its properties are not yet initialized, it just looks like a temporary fake object. If the string is not found, it will throw an ObjectNotFoundException.

  • session.get ()

    It always gets into the database and returns a real object, an object representing a database row, not a proxy. If the string is not found, it returns null.

+3
source

When you call the session.load () method, it always returns a proxy object, what does the value of the proxy object mean? Proxy means that hibernate will prepare some fake object with the given identifier value in memory without getting into the database, for example, if we call session.load (Student.class, new Integer (107)); > hibernate will create one fake Student [row] object in memory with identifier 107, but the rest of the Student class properties will not even be initialized. enter image description here

Get

When you call session.get (), it immediately enters the database and returns the original object. If the row is not available in the database, it returns null.

+2
source

hibernatesession.get () will retrieve the real object from the database, and hibernatesession () will return the proxy server without getting into the database. Click here for more details. He explains the method of getting and loading and their difference with code examples.

+1
source

** Load: ** Each time the load () method is called, hibernate creates a proxy object of the POJO class and sets the id for the proxy object, and then returns the proxy object to the program. Based on the operations performed with the proxy object, hibernate will decide whether to go to the cache or database to load data. This process is called lazy loading.

** Get: ** When we call the get () method, hibernation first goes to the first level cache, and if this object does not exist in the first level cache, it goes to the database and loads the object from the database. If Id does not exist in the database, the get () method returns zero. When the get () method is called, the proxy object is not created, so it is called as an early load.

Link: http://docs.jboss.org/hibernate/orm/4.3/javadocs/

You can find a complete example on my blog: http://www.onlinetutorialspoint.com/hibernate/hibernate-session-differences-between-load-and-get.html

+1
source

Here is a simple explanation of what you need: http://www.mkyong.com/hibernate/different-between-session-get-and-session-load/

Or by looking at the API: http://docs.jboss.org/hibernate/orm/4.3/javadocs/

with get : return a persistent instance of this named object. Permanent, so it is stored in the database.

with load : read the constant state associated with this identifier into this transient instance.

In the first link you will find very useful examples for checking differences.

0
source

Loading: when we call the session.load () method, it does not directly go to the database. It creates and returns a proxy object, if the object does not belong in db, it throws an "ObjectNotFountException". And supports lazy loading.

Get: it gets directly to the object in db and gives the original value, if the object is not found, then it returns null. And it supports download.

0
source

From the above example, both functions do not work the same.

load () - hibernate will only load the proxy of the object with the specified ID. All properties will not be set in advanced mode. Once you call getter methods on an object, the request will be a problem. Therefore, when you can use the getAccount method, a query is selected and the result object will be stored in the cache as it is received by identifier. Thus, any subsequent calls through get will not invoke any select statement.

get () - will always retrieve an object from the database with filled properties. For properties defined in collections, depends on the lazy initialization configuration. Please note: any subsequent calls in the same session return an object only from the cache, and therefore, requests will not be returned. This is what happens when the get method is called the first time, and the second time the object is ready in the cache by calling acc.getBalance () on the object obtained at boot time.

0
source

Remember the performance aspect between get and load

The get () method retrieves data as soon as it is executed when the load () method returns a proxy object and retrieves only data when properties of the object are required. So the load () method gets better performance because it supports lazy loading. Whe should use the load () method only when we know the data, because it throws an exception when the data is not found.

You can see an example demonstrating this difference in the tutorial The difference between the get and load method in Hibernate

0
source

Use load:

  • If you are confident in the availability of the object that you receive from the database. Otherwise, you will end up catching an ObjectNotFoundException.

  • When you have a heavy object to load (since it loads lazily whenever you use it)

Use get:

  • If you are not sure about the availability of the object in the database.
  • You get the opportunity to check for null when there is no object in the database.
  • When you have a lightweight object to download (since it loads eagerly).
0
source

When you call the session.load() method, it always returns a proxy object. What does a proxy object mean? Proxy means that hibernate will prepare some fake object with the given identifier value in memory without affecting the database, for example, if we call.

  session.load(Student.class,new Integer(107)); 

hibernate will create in memory one fake Student [row] object with identifier 107, but the rest of the Student class properties will not even be initialized, pay attention to this graphical representation ...

It will get into the database only when we try to get other properties of the Student object, I mean stdName , stdCountry .
If we call s2.getStdName() then hibernate will go to the database, s2.getStdName() search for the line with student ID 107 and get values; if object [row] not found in the database, it will ObjectNotFoundException .


  session.get() 

When you call session.get() , it immediately enters the database and returns the original object. If the row is not available in the database, it returns null .


So which method is best to use, hibernate load() or get() ? It is completely your choice.

0
source

All Articles