-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I determine if something is in the cache?
PostPosted: Mon Jul 31, 2006 4:48 pm 
Newbie

Joined: Mon Apr 10, 2006 10:33 am
Posts: 7
Location: LA
Hibernate version:3

I'd like to know if there's a method that will allow me to check if a certain object is cached or not.

Something similar to the method below of the Session:

Code:
Object   get(String entityName, Serializable id)


But I don't want it to pull from the database - just return the cached object (if there IS one) or null perhaps if there isn't.

I'm not even sure how to GET to the cache directly (so thoughts on that might be welcome) or if the id accepted by Session is the same thing used by the cache as it's key - probably not.

But you get the idea right? How can I tell if a certain entity/id is in the cache without forcing it to LOAD the thing (which I do not want to waste time doing).

Thanks!

Havrilesky


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 5:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can use session.contains(new YourEntity(theId)), that will return true if the entity is in the session cache.

You can get unrestricted access to EhCache, hibernate doesn't encrypt its cache entries. Have a look at http://ehcache.sourceforge.net/ for the docs.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 3:51 pm 
Newbie

Joined: Mon Apr 10, 2006 10:33 am
Posts: 7
Location: LA
Well, that didn't seem to work.

I guess the idea is that the Session knows that 2 instances of YourEntity that have the same id property ought to be equivalent?

That makes sense but it doesn't seem to work for me.

(when I try session.getIdentifier(anEntity) I *do* get a string that I recognize as being the ID for that entity)

It's just that when I make a new instance of YourEntity using the same ID for the one I just queried from the database, session.contains returns false.

could this have something to do with the way the id is defined in the .hbm document? (although it seems straight-forward enough to me):

Code:
      <id name="itemID" type="string" column="itemID">
         <meta attribute="scope-set">protected</meta>
         <generator class="native"/>
      </id>


Not sure what to try next. Any advice most welcome!

Havrilesky


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 5:58 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There are some restrictions on this that I should have pointed out last time.

session.contains makes use of hashCode, so this trick isn't suitable for entities that correctly define hashCode using a business key. For entities that can't exist in hashed sets, you can use the DB id as the hashCode, and session.contains(new YourObject(youdId)) will work.

For entities with a "good" hashCode, you'll have to find some other way of doing this. Can't think of a solution right now.. you could try session.lock(yourObj, LockMode.NONE) before session.contains(yourObj), but I don't think that'll do it. Worth a shot though.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.