-->
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: Help with Query Caching/natural id
PostPosted: Mon Apr 09, 2012 10:27 pm 
Newbie

Joined: Mon Apr 09, 2012 10:00 pm
Posts: 1
Hi,

Sorry I am a newbie w.r.t hibernate and I need some help in understanding how to define natural-id for my table.

My database structure is as follows

provisionId auto-increment (pk)
name varchar(255) not null
dept varchar(255) not null
D , E, F some other fields..

(name,dept) Unique

My queries are lookups based on (name,dept).

I am trying to use hibernate query caching to cache my queries so I don't have to go to the database all the time. From what I read online, for query cache to be effective, I want to define a natural-id which referes to a unique row in the table and as long as the natural-id doesn't change, any updates to a row will not result in invalidation of my query cache. So I can update (id,D,E,F) and when I query with (A,B) the value should come from cache.

In order to do this in my hibernate config file, I have

<cache usage="read-write"/>
<id name="provisionId" type="int" column="provision_id" unsaved-value="null">
<generator class="native" />
</id>
<natural-id>
<property name="name" type="java.lang.String"
column="name" length="255" />
<property name="dept" type="java.lang.String"
column="dept" length="255" />
</natural-id>

When I do the query I use

crit.add(Restrictions.naturalId().set("name", name));
crit.add(Restrictions.naturalId().set("dept", dept));
crit.setCacheable(true);
return crit.uniqueResult();


This works for the tests I have done. Do you see any issues with this?

My next issue is I want to add a new column maildrop to my existing table such that (name,maildrop) is unique. I would also want to query based on (name,maildrop) and I want it to work same as (name,dept) with respect to query cache.

Can someone tell me how do i add this to my xml file? If I add to the <natural-id> block wont it become unique(name,dept,maildrop) which is not what I want.

I would appreciate if you could give me any pointers... Thanks.


Top
 Profile  
 
 Post subject: Re: Help with Query Caching/natural id
PostPosted: Tue Apr 10, 2012 9:03 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
This works for the tests I have done. Do you see any issues with this?


No, I don't see any issues with this.
The unique thing to notice is, that since Hibernate4.1 the NaturalId feature has become a big enhancement,
for example you don't need to use the second level query cache anymore,
see http://in.relation.to/Bloggers/41FeatureLoadingByNaturalId

Quote:
My next issue is I want to add a new column maildrop to my existing table such that (name,maildrop) is unique.


You can do that, but not with the NaturalId feature.
You cannot define multiple sets of NaturlalId's for a single entity class or a class hierarchy (HHH-7129).


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.