-->
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.  [ 1 post ] 
Author Message
 Post subject: Trying to understand the L2 cache, NamedQueries vs. em.find
PostPosted: Tue Jul 13, 2010 9:21 am 
Newbie

Joined: Fri Mar 12, 2010 9:42 am
Posts: 19
Hi guys
I have made a basic example in order to try to understand how the L2 cache in Hibernate is working. I am using JPA2 with the latest Hibernate Entitymanager, and EhCache as cache provider.

My example first finds and object by its primary key in the entity manager, and then does the same thing using a named query:

Code:
ElectionType electionType = em.find(ElectionType.class, 1L);

Query query = em.createNamedQuery("ElectionType.findByPk").setParameter("pk", 1L);
electionType = (ElectionType)query.getSingleResult();


The named query is defined like this:
Code:
@NamedQueries({
   @NamedQuery(name = "ElectionType.findByPk", query = "SELECT et FROM ElectionType et WHERE et.pk = :pk")
})


On the first execution of em.find() I get a cache miss debug statement, and Hibernate executes a select query on the object.
On the first execution of the named query, I get no cache debug statements, and Hibernate executes the same select query.

On the second execution of em.find() I get a cache hit and no sql, but on the second execution of the named query, the same select query executes again.

I am wondering, shouldn't Hibernate be able to execute the named query on its cache only?

If not then I guess this is where the query cache comes in. Does it then mean that all named queries will be executed against the database if the query cache is disabled?

I have also tried enabling the query cache in my persistence.xml:

Code:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="evotePU">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>jdbc/myPU</jta-data-source>
      <properties>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.cache.use_second_level_cache" value="true" />
         <property name="hibernate.cache.use_query_cache" value="true" />
         <property name="hibernate.cache.use_minimal_puts" value="true" />
         <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
         <property name="hibernate.cache.provider_configuration_file_resource_path" value="META-INF/ehcache.xml" />
         <property name="hibernate.generate_statistics" value="true"/>
      </properties>
   </persistence-unit>
</persistence>


But it doesn't seem to have any effect. Is there anything else I have to do to mark this query as cacheable? Haven't found any parameters in @NamedQuery.

All this is probably obvious for experienced Hibernate users, but a newbie would really appreciate help in clarifying things here :)

Regards,
Anders


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.