What is the difference between get and load method in hibernate




















By using the load or get method we can load read the data from the database. The load and get methods are both looking like a same, and also these two are generates same output. But the functionality wise there are few differences in internally. In this tutorial we will discuss all possible differences between load and get methods with examples. Before going to discuss about the differences directly, It is mandatory to know what exactly are the load and get methods and how they are working internally.

In order to get the details from the database, we can use the load method. The load is a instance method which is coming from the hibernate Session object. We can call the load with two parameters.

By using the above two methods we can load the data from the database. When ever the load method is called, the hibernate creates a proxy object of a POJO class provided as parameter , and it will set the id to the proxy object, then it returns the proxy object to the program. Based on the operations performed on the proxy object, the hibernate will decide whether to go cache or database to load the data.

It is slower than load because it return fully initialized object which impact the performance of the application. If you are not sure that object exist then use get method.

If you are sure that object exist then use load method. Himanshu shriv. Previous Page Print Page. Next Page. If no row is found, a ObjectNotFoundException will throw. Get returns the object by fetching it from database or from hibernate cache whereas load just returns the reference of an object that might not actually exists, it loads the data from database or cache only when you access other properties of the object.

With load , we are able to print the id but as soon as we try to access other fields, it fires database query and throws org. ObjectNotFoundException if there is no record found with the given identifier.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What's the advantage of load vs get in Hibernate? Ask Question. Asked 10 years, 7 months ago. Active 3 months ago. Viewed 65k times. Antonio Antonio Add a comment. Active Oldest Votes. Thanks for your answer. So, I understand that load returns an Author object whose id fields have values, but every non-id field like author name, author country etc.

Is that correct? Also, when your transaction is not yet committed, and you try to access a null field, hibernate will issue a query to fetch and set only the requested field? Or will it fetch and set all the null fields? Please tell me. BoratSagdiyev accessing null field of a proxy returned by load will trigger DB accessing to fetch all fields not only the requested field , see this - stackoverflow. Exactly the answer I was looking for! Whats the advantage of load vs get in Hibernate?

If you are not sure that the object exist, then use one of get methods. Premraj Premraj From the "Java Persistence with Hibernate" book, page The one difference between get and load is how they indicate that the instance could not be found.

Ravi Dmitry Sobolev Dmitry Sobolev 15 15 silver badges 18 18 bronze badges. Satya Satya 7, 9 9 gold badges 35 35 silver badges 40 40 bronze badges. GuruKulki GuruKulki The performance issues is also major difference between get and load method.



0コメント

  • 1000 / 1000