-->
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: natural-id lookup
PostPosted: Tue Feb 06, 2007 6:28 pm 
Newbie

Joined: Mon Aug 23, 2004 7:54 am
Posts: 15
Hibernate version:
3.0

I have a question regarding the second level cache.
What is the difference between:

Code:
Criteria crit = hSes.createCriteria(UniExtProcedure.class);
      crit.add(
         Restrictions.eq("myBKey", "b")
      ).setCacheable(true);


and

Code:
Criteria crit = hSes.createCriteria(UniExtProcedure.class);
      crit.add(
         Restrictions.naturalId().set("myBKey", "b")
         ).setCacheable(true);

(in the second case, the mapping is correct, using natural-id lookup).

When doing a detailed log, the key for the L2 cache looks very similar in both cases and the only way to hit the L2 cache is to repeat the same query twice (for the first and second case)
This is logical for the first case, but for the second one Hibernate has some idea about the meaning of the lookup key.
Is there something similar to the load(class, id) from the L1 cache to help me to the native-id lookup in another way?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 8:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want to use only the L1 cache, then you can't use queries. Query results are stored in the L2 cache, as individual fields: the L1 cache stores complete objects. You can either use hSes.get(UniExtProcedure.class, "b") (uses L1 cache), or one of the queries above (which use the L2 cache).

You could change your criteria query to use a projection to return only the id (thus storing only the id the L2 cache), then use the id to look up the object in the L1 cache.

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


Top
 Profile  
 
 Post subject: Re: natural-id lookup
PostPosted: Wed Feb 07, 2007 1:24 am 
Newbie

Joined: Mon Aug 23, 2004 7:54 am
Posts: 15
Ok...

If I use get(Class, id), the id must be the pk... it doesn't seem to work with natural id's. So no advantage here...
I really don't see the advantage of using natural-id. For L1 cache can't be used and for L2 cache a select key corresponding to the query result will be generated like in my first SQL (the one with Restrictions.eq()).

My problem was that I insert objects that have such a natural key. And at a later moment in time I need to fetch them using that key. I previously used a map, and I hoped that Hibernate could give me an alternative.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 1:53 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's true, natural-ids are mostly useful only for self-documentation. However, you should still be seeing some benefit to using Restrictions.naturalId(). While the query is undoubtedly going back to the L2 cache, it should still be taking some shortcuts once it gets there. They might not be apparent from your logging, but the internal cache lookup is probably more efficient.

If there's anyone out there who knows more about what happens when Restrictions.naturalId() is used, I'd be keen to hear about it too. I rarely bother to define natural ids: I'd put more effort into it if I knew what it was that they help with.

_________________
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.