File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Get v/s Load method in Hibernate - Why two methods are provided? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Get v/s Load method in Hibernate - Why two methods are provided?" Watch "Get v/s Load method in Hibernate - Why two methods are provided?" New topic
Author

Get v/s Load method in Hibernate - Why two methods are provided?

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
Hi,

There are two methods available in Hibernate to retrieve the objects from the database:

1. Get Method
2. Load Method

Get method returns null when object is not present in the database and Load method throws an exception.
I am wondering for:

1. What is the benefit of defining these two methods?

2. We could have just one method instead of two and would have handled the scenarios accordingly, so, what is the benefit actucally we are getting by defining two methods?

Thanks,
Vaibhav Garg
Santosh Manikandan
Greenhorn

Joined: Sep 11, 2011
Posts: 6



will return a fully loaded object of the User entity with userId = 1 if found. it returns null otherwise



will return a proxy User object with the identifier property populated without hitting the database. It hits the DB only when you attempt to access a non-identifier property and it is then that it throws an exception if a record with userId = 1 is not found. Does not throw an exception while you are accessing the identifier property only.

Now, given a scenario where in a e-commerce system User is creating a Bid. Bid has a one-to-one association with User. When a Bid object is created, its user attribute needs to be populated with a User object. When the Bid object is subsequently saved in the Bid table the foreign key reference to User table needs to be set. Given this, you may want to ask yourself ; do you really want a fully loaded User object in Bid just for setting the value of a foreign key in Bid table OR is it more than sufficient to achieve the same thing with a proxy.


------------------------------------------------------------
Think, Learn, Teach, Invent, Innovate, Build, Deliver
------------------------------------------------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get v/s Load method in Hibernate - Why two methods are provided?
 
Similar Threads
Loading a servlet
Hibernate load vs get, confused on Hibernate Proxies too
Questions on Entity Bean
Static Methods
Static Method Inheritance