Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

load() vs get()

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to hibernate.
I was searching the difference between load() and get() and this is what I got :
First, when the given ID can’t be found, load() throwsan exception org.hibernate.ObjectNotFoundException, whereas get() returns a null object.
Second,load() just returns a proxy by default; the database isn’t hit until the proxy is first invoked. get() hits the database immediately. The load method is useful when you only need a proxy and don’t need to make a database call. You just need a proxy, when in a given session you need to associate an entity before
persisting.

I didn't understand the second point. Here what is meant by proxy? Could you please elaborate it?
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A proxy is a representation of the "real" object. By default Hibernate creates a proxy for each of the class you map in mapping file. The proxy contains actual method to load the data. when any of the methods in the class are called then the "real" object is touched. You can also look up "lazy Loading/fetching" for more explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic