-->
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: L2 cache indexed by non @Id field
PostPosted: Tue Jul 21, 2009 5:23 pm 
Newbie

Joined: Tue Jul 21, 2009 10:52 am
Posts: 2
I'm using tomcat+hibernate+ehcache

I haven't been able to find a definitive answer as to whether named queries will pull from the L2 cache if the where clause does not use the id of the class. In the following class, my system always goes to the DB for "Group.getByHandle". (all the annotations are JPA except the @Cache)

Is there a way to make this happen?

Code:
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "ss_group")
@NamedQueries({
    @NamedQuery(name = "Group.getById",     query = "SELECT g FROM Group g WHERE g.id=?1"),
    @NamedQuery(name = "Group.getByHandle", query = "SELECT g FROM Group g WHERE g.handle=?1"),
})
public class Group implements Serializable {
    @Id
    @Column(nullable = false, updatable = false)
    protected String id = UUID.randomUUID().toString();

    // unique name of group
    protected String handle = null;
}


Top
 Profile  
 
 Post subject: Re: L2 cache indexed by non @Id field
PostPosted: Wed Jul 22, 2009 6:32 pm 
Newbie

Joined: Tue Jul 21, 2009 10:52 am
Posts: 2
Query cache!

So this was not really clear to me from reading the hibernate documentation.

In order for hibernate to map query parameters to id so that it can retrieve entities from the second level cache, the queries need to be cached in the query cache (which they are not by default).

So I added all my settings in persistence.xml to enable all the caches but only my queries by id were served from the L2 cache.

Since I'm using JPA, adding the following hint to my named queries enabled the query results to be query cached in the query cache.

Code:
@NamedQuery( name=..., hints={@QueryHint(name="org.hibernate.cacheable",value="true")})


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.