• 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

Loading proxy without hitting database

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is possible if the object is coming from the second-level cache or first-level cache session.
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Got it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic