aspose file tools*
The moose likes Object Relational Mapping and the fly likes Loading proxy without hitting database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Loading proxy without hitting database" Watch "Loading proxy without hitting database" New topic
Author

Loading proxy without hitting database

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Whenever Hibernate returns an instance of an entity class, it checks whether it can return a proxy
instead and avoid a database hit.

I wanted to understand that in the first place, how does the proxy get the unique id associated with entity object. It has to eventually come from db. right?

Please clarify how is the id populdated in proxy.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Yes, it is possible if the object is coming from the second-level cache or first-level cache session.

If you are not laughing at yourself, then you just didn't get the joke.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
What if none of this is true and the object has to be fetched from database.

load() method will throw an exception if the unique id is not found in the database. So does it mean that the load API has to hit db to make sure that the id exist in db. If yes, then what does it mean that "When proxy is loaded, it does not hit db "
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Exactly from where are you quoting this.
Can you share the source, so that we can take a look at it.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I found the following lines from Java Persistence with hibernate, topic 13.1.3 Understanding proxies.
My question is that how does hibernate know if there is an entry for Item with Id 123 in database table without actually hitting the database.

Proxies are placeholders that are generated at runtime. Whenever Hibernate
returns an instance of an entity class, it checks whether it can return a proxy
instead and avoid a database hit. A proxy is a placeholder that triggers the loading
of the real object when it’s accessed for the first time:




The third line in this example triggers the execution of the SQL that retrieves an
Item into memory. As long as you access only the database identifier property, no
initialization of the proxy is necessary. (Note that this isn’t true if you map the
identifier property with direct field access; Hibernate then doesn’t even know that
the getId() method exists. If you call it, the proxy has to be initialized.)
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513


As long as I'm doing a item.getId(), I don't have to go to the database right?
Only when you're doing, item.getDescription(); we actually need some data which is not in any of the cache and hibernate has to fire a SQL and fetch the information.
Now, where are you confused?
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Ok. Got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading proxy without hitting database
 
Similar Threads
instanceOf in hibernate
oracle database view+entity
load() vs get()
JPA reference OneToOne CascadeType problems
Standalone Persistence example with bug?