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 load() vs get() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "load() vs get()" Watch "load() vs get()" New topic
Author

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
persisting.

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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: load() vs get()