File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes load() vs get() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "load() vs get()" Watch "load() vs get()" New topic

load() vs get()

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
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

I didn't understand the second point. Here what is meant by proxy? Could you please elaborate it?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: load() vs get()
jQuery in Action, 3rd edition