-->
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.  [ 2 posts ] 
Author Message
 Post subject: Does second level cache work for both natural and primary ID
PostPosted: Fri Feb 22, 2008 4:12 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
Has anybody done anything like this an shed light on it? Any input would be appreciated. thanks.

First, 2 classes, and a mapping. The key thing is a many-to-one relationship single ended, from one to the other.

Code:
    public class First{
      Long ID;
      Long Name;
      Second second;
    }
    public class Second{
      Long ID;
      String code;
      Long location;
      String other;
    }

Code:
<class name="Second" dynamic-update="false" table="SECONDS">
      <cache usage="read-write"/>
      <id name="ID" column="ID" type="long">
         <generator class="native"/>
      </id>
      <natural-id mutable="false">
         <property name="code" column="CODE"/>
         <property name="location" column="LOCATION"/>
      </natural-id>
      <property name="other" column="OTHER"/>
   </class>
   <class name="First" table="FIRSTS">
      <id name="ID" column="ID" type="long">
         <generator class="native"/>
      </id>
      <many-to-one name="second" class="Second" column="SECOND_ID"/>
      <property name="name" column="NAME"/>
   </class>


Will second level cache support both Primary and Natural/Business Key Lookup. It doesn't appear so.

Also, i notice that when i insert First, it will issue a SQL Insert, then an Update on it to update the FK for Second, even if Second exists (that is you won't even see an insert of Second, though you will see the select). This only happens in this many-to-one relationship, all others in our system sequence it properly. Not sure if it relates to the cache and the business key.

Cache usage is as follows:

We use the naturalID at runtime to do cached lookups, then we store the FK relationship using the primary key along with any containing object.

So when we do a cached natural key query, and it is placed in the second level cache, will it also be in second level by primaryID? If not, is that a restriction?

Like thus:
Code:
          Second cached = (Second)hsess.createCriteria(Second.class))
          .add( Restrictions.naturalId()
              .set("code", “acode”) // discriminator
              .set("location", “allocation”)  //code
          ).setCacheable(true).setFlushMode(FlushMode.MANUAL)
          .uniqueResult();


So, say I reteive a Second with ID = 2

Will a hsess.load(Second.class, new Long(2)); // pull this from cache?
Hence will this cache be used when writing out First and resolving the FK?
We have a small number of Seconds, but they are used all over the place, so this is what we need.

_________________
Please rate this reply if it helps!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 7:55 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
pgrillo,

Caching a query provides a small, usually negligible performance gain in terms of query compilation, but this is unrelated to the cache of object instances. The second-level cache is concerned exclusively with what you load with the load/get operations.

And yes, the second-level cache should work independently of the type of key (generated or assigned) that you use. You can ask for caché statistics in order to check if, the second time you call load, the data comes from the DB or from the local cache.

_________________
Gonzalo Díaz


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