• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic