-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Checking whether entity objects are in the cache
PostPosted: Thu Mar 12, 2009 4:41 pm 
Newbie

Joined: Wed Mar 04, 2009 6:36 pm
Posts: 8
Hi all,

in our application, we are doing a manual prefetching of entity objects which we know we will be using. We do so based on the entity's IDs using a simple HQL query like the following:
Code:
from Entity e where e.id in (:ids)

The drawback is that some of the entities loaded by the query may already be in the cache so loading them again makes no sense. Therefore, we want to exclude the entities which are already in the cache from the query.

My question is how I can check whether an entity is already loaded in the 1st or 2nd level cache if I only have the entity's ID? I already know of the Hibernate#isInitialized method but there I need to supply the entity object itself as parameter and not its ID. Also, first getting the entity object through Session#get(Class entityClazz, Serializable id) and then calling Hibernate#isInitialized does not work if the entity class does not allow proxies to be created by Hibernate since then, calling Session#get would already trigger a database query in case the entity object was not loaded yet.

Any idea how to achieve this? Thanks in advance for any help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 5:54 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Does http://opensource.atlassian.com/projects/hibernate/browse/HHH-3665 help you?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 4:21 pm 
Newbie

Joined: Wed Mar 04, 2009 6:36 pm
Posts: 8
@mmerder: Thanks for the link but what I want is rather a method where I can check whether an entity object is cached and initialized, based on the ID and the entity class only i.e. without requiring the actual entity object (or its proxy) as the method Session#contains does. So, what I'm essentially looking for is a method like:
Code:
Session#isInitialized(Class entityClazz, Serializable id)

The reason why I want to do the lookup based on the ID only is that if my entity class does not allow Hibernate to create any proxy for it, I see no way to get a reference to my entity object without already querying it from the database.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 6:37 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, you could use the solution posted in the mentioned jira issue. The method getEntityUsingInterceptor is public in SessionImpl, so you could cast your session to SessionImpl:
Code:
((SessionImpl)session).getEntityUsingInterceptor(new EntityKey(id, getFactory().getEntityPersister(entityClass.getName()), getEntityMode())) != null;


Thats not very nice, but solves your problem. Rating is welcome. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 3:38 pm 
Newbie

Joined: Wed Mar 04, 2009 6:36 pm
Posts: 8
mmerder wrote:
Well, you could use the solution posted in the mentioned jira issue. The method getEntityUsingInterceptor is public in SessionImpl, so you could cast your session to SessionImpl:
Code:
((SessionImpl)session).getEntityUsingInterceptor(new EntityKey(id, getFactory().getEntityPersister(entityClass.getName()), getEntityMode())) != null;


Thats not very nice, but solves your problem. Rating is welcome. ;-)

Actually, at least I know how I could do it and I also know that there seems to be no other way to achieve this, so... +1 :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.