Take the 2-minute tour ×

How can I find out if a certain HibernateProxy is in the first level cache?

The code is roughly

for (HibernateObject obj : objects) {
    if (HibernateUtils.isHibernateProxyInCache(
    ) {
    } else {
        display("click here to see expensive lazy object");

the code I need is HibernateUtils.isHibernateProxyInCache, but I don't know how to implement it.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I think you're looking for Hibernate.isPropertyInitialized.

Calling HibernateUtils.isHibernateProxyInCache(obj.getExpensiveLazyObject()) would initialize the proxy to the expensive lazy object, so your method would always return true.

share|improve this answer
are you saying that simply calling getX would initialize the proxy? Can you please give me a link to the piece of documentation stating that? So if I understand you correctly, I need to do Hibernate.isPropertyInitialized(obj,"expensiveLazyObject"), right? –  Chi-Lan Oct 24 '11 at 7:54
Sorry, I misunderstood. You probably just need Hibernate.isInitialized(obj.getExpensiveLazyObject()). –  JB Nizet Oct 24 '11 at 9:01
thanks. But I'm not sure I understand what "initialized" mean. Can you refer me to the docs? I'm not sure that "initialized" means "in first level cache". –  Chi-Lan Oct 24 '11 at 9:11
initialized means : "the proxy has been initialized". Initially, a proxy to an entity only holds the entity's ID. Once you call any method on the proxy object, it first loads the actual entity instance, with all its fields, from the database, and then delegates to this entity instance to execute the method. Initialized means "the entity state has been loaded from the database". If the proxy is still attached to its Hibernate session, then an initialized proxy should be in the cache. If it's not what you need, then please elaborate, and look at the method contains in Session. –  JB Nizet Oct 24 '11 at 9:20
add comment

Your Answer


