• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic