-->
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: Criteria API and the second-level cache
PostPosted: Wed Nov 28, 2007 6:54 am 
Newbie

Joined: Wed Nov 28, 2007 6:34 am
Posts: 1
Hi,

Are the following examples the only ways to hit the second-level cache?

1.
Code:
User user = (User) session.get(User.class, new Long(1));

2.
Code:
Iterator users = session.createQuery("from User u where u.firstName = :firstName").setLong("firstName", firstName).iterate();


Why doesn't the following code example hit my second-level cache (ehcache 1.3.0)?

Code:
User user = (User) session.createCriteria(User.class).add(Restrictions.idEq(new Long(1))).uniqueResult();


The Criteria API should have all the information it needs to query the second-level cache, i.e. the identifier and the User class.




Hibernate version: 3.2.5ga

Mapping documents:
<class name="xxx.User" table="user" lazy="true">
<cache usage="nonstrict-read-write" />

<comment>Hibernate Mapping for User</comment>

<id name="id" column="id">
<generator class="native" />
</id>
<property name="alias" column="alias" />
<property name="createdTime" column="created_time" type="timestamp" />
<property name="email" column="email" />
</class>
Name and version of the database you are using: MySQL 5.0.42 (Linux CentOS)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 11:33 am 
Newbie

Joined: Fri Jan 07, 2005 4:50 pm
Posts: 1
Location: Edinburgh
I've come to the same conclusion myself - the criteria API never hits the second level query cache

By using <property name="hibernate.show_sql">true</property>, I can see whether a cache hit occurs.

Repeated calls of this code hit the query_cache
String queryString = "select s1 from PeriodNomination as s1 where s1.fieldGroupName = ? and s1.nominationDate = ? and s1.nominationHour = ? order by s1.nominationHour";
final List<PeriodNomination> list = getHibernateTemplate().find(queryString, new Object[] {fieldGroupName, nominationDate, nominationHour});
if (list.size() > 1) throw new RuntimeException();
return list.size()<=0?null:list.get(0);

Repeated calls of this code DON'T hit the query_cache
Criteria criteria = getSession().createCriteria(PeriodNomination.class);
criteria.add(Restrictions.eq("fieldGroupName", fieldGroupName));
criteria.add(Restrictions.eq("nominationDate", nominationDate));
criteria.add(Restrictions.eq("nominationHour", nominationHour));
return (PeriodNomination) criteria.uniqueResult();


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.