-->
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: Query cache and insert
PostPosted: Wed May 24, 2006 6:21 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 5:33 am
Posts: 31
I have the following simple method:


public static void main(String[] args) {
HibernateUtil.beginTransaction();
Session session;
session = HibernateUtil.getCurrentSession();

for (int i = 0; i <10; i++) {
Person person = new Person();
person.setNickName( "test" + i );
session.saveOrUpdate( person );

List list = session.createCriteria( Person.class )
.add(Restrictions.eq( "nickName", "homer" ))
.setCacheable(true)
.list();
}

HibernateUtil.rollbackTransaction();
HibernateUtil.closeSession();
}

This makes the query cache unusable, since Hibernate won't know if the resultset of the query has changed, and therefor executes a new query.

The class Person is cachable with strategy NONSTRICT_READ_WRITE

What I would like to do is to append the "person" instance to the query cache, so that hibernate would know the resultset of the query. Is that possible?

I am using EhCache 1.2, Hibernate 3.2.CR2 and Hibernate Annotations 3.2.0 CR1


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 8:23 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 5:33 am
Posts: 31
According to the docs:

For most queries, including criteria queries, the query cache is not very efficient, because query cache invalidation occurs too frequently. However, there is one special kind of query where we can optimize the cache invalidation algorithm: lookups by a constant natural key. In some applications, this kind of query occurs frequently. The criteria API provides special provision for this use case.

First, you should map the natural key of your entity using <natural-id>, and enable use of the second-level cache.

<class name="User">
<cache usage="read-write"/>
<id name="id">
<generator class="increment"/>
</id>
<natural-id>
<property name="name"/>
<property name="org"/>
</natural-id>
<property name="password"/>
</class>

Note that this functionality is not intended for use with entities with mutable natural keys.

Next, enable the Hibernate query cache.

Now, Restrictions.naturalId() allows us to make use of the more efficient cache algorithm.

session.createCriteria(User.class)
.add( Restrictions.naturalId()
.set("name", "gavin")
.set("org", "hb")
).setCacheable(true)
.uniqueResult();


The problem is that I am using annotations instead of hbm files, and it seems that natural key isn't supported by annotations yet.

And I can't mix mapping strategies between annotations and hbm files, can I?


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.