-->
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.  [ 7 posts ] 
Author Message
 Post subject: Additional indices for the cache?
PostPosted: Tue Sep 28, 2004 3:45 pm 
Newbie

Joined: Tue Sep 28, 2004 3:20 pm
Posts: 2
Syntetic primary keys are widely used and they make sense. In some circumstances they are slow, let me describe an example. If you have a user management for a web site. The natural key might be the login name. The syntetic key might be a user identifier (number). If you validate a user you have to go to the database even if your user is cached because you don't know the primary key.
To avoid it I implemented an LRU cache with a map to resolv the natural key -> primary key. I think it would be be a better solution if Hibernate would do that. All you need is to tag the attribute as unique. Hibernate could use unique attributes to provide additional indices for the cache.
Is something planned like that for release 3?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 3:49 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This doesn't make much sense, since you would have to hit the databse in any case to do this validation. The cache only has a subset of data.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 9:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually it can make sense, when resolving associations with property-refs, and in a future version of Hibernate, we *may* support cache by natural key (I havn't thought through all performance implications yet) but I agree that in this particular case it doesn't make sense.

You have to make sure at *some* stage that the user exists, which requires a trip to the db. You would then cache the detached User object in the HttpSession, so you will always know its surrogate key from then on.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 7:53 am 
Newbie

Joined: Fri Mar 12, 2004 10:02 am
Posts: 16
I don't think a DB roundtrip is necessary. Why not first ask the cache if the object is cached and only if it's not cached you need a DB roundtrip. This works seamless at least for data you are the only modifier.


Top
 Profile  
 
 Post subject: Did this feature make it in to 3.0?
PostPosted: Mon May 16, 2005 11:39 pm 
Newbie

Joined: Thu Jan 27, 2005 12:26 pm
Posts: 5
Did this feature make it in to 3.0?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 16, 2005 11:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
no


Top
 Profile  
 
 Post subject: Scenario
PostPosted: Tue May 17, 2005 12:02 am 
Newbie

Joined: Thu Jan 27, 2005 12:26 pm
Posts: 5
Thanks for the quick response! I'd like to outline my scenario and why I would like this feature, for posterity. Maybe a justification to include in 4.0.

I dutifully used surrogate keys as the primary identifiers for all my objects, as recommended in the wonderful book Hibernate in Action.

Our web site is internationalized, and one of the objects is a TranslationDomain, which has a property "code" that identifies it, that is also a natural key (unique=true). The web view layer makes calls to the service layer getTranslationDomainByCode("LoginPage") and getTranslationDomainByCode("PurchasePage"), etc, which it can then query by Locale to get the proper localized String to display.

These TranslationDomain objects are prime candidates for caching, since they rarely change. The problem is, the view layer only knows the codes (i.e. the natural keys), so it's missing the cache hit each time.

Of course, the view layer can maintain its own local cache of Translations, but... the Translations WILL change sometimes, (i.e., if we find a typo and want to correct it). So that's where the cache timeout provided by an ehcache would be awfully handy so we don't have to restart the web server to have the translation take effect! :-)

So, in closing, unless I'm missing an obvious solution here, it would just be really nice if hibernate could somehow use natural keys to identify cache hits.

Thanks for listening!


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