-->
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 works with HQL but not with Criteria
PostPosted: Thu Apr 16, 2009 4:24 pm 
Newbie

Joined: Mon Apr 14, 2008 2:01 pm
Posts: 4
Hibernate version: 3.2.5

If I use the following HQL query everything works like a charm:
Code:
Query query = session.createQuery("from Employment where since > :now");
query.setDate("now", new Date() );
query.setCacheable(true);

Cache statistics:
Code:
org.hibernate.stat.StatisticsImpl  - query cache puts: 1
org.hibernate.stat.StatisticsImpl  - query cache hits: 3
org.hibernate.stat.StatisticsImpl  - query cache misses: 1

Generated SQL:
Code:
    select
        employment0_.id as id5_,
        employment0_.companyName as companyN2_5_,
        employment0_.since as since5_,
        employment0_.till as till5_
    from
        Employment employment0_
    where
        employment0_.since>?


But with the criteria API it won't work:
Code:
Criteria criteria = session.createCriteria(Employment.class);
criteria.add(Restrictions.gt("since", new Date()));
criteria.setCacheable(true);

Cache statistics:
Code:
org.hibernate.stat.StatisticsImpl  - query cache puts: 4
org.hibernate.stat.StatisticsImpl  - query cache hits: 0
org.hibernate.stat.StatisticsImpl  - query cache misses: 4

Generated SQL:
Code:
    select
        this_.id as id5_0_,
        this_.companyName as companyN2_5_0_,
        this_.since as since5_0_,
        this_.till as till5_0_
    from
        Employment this_
    where
        this_.since>?

Is it a bug or am I missing something ?


Top
 Profile  
 
 Post subject: Re: Query cache works with HQL but not with Criteria
PostPosted: Sat May 02, 2009 7:47 am 
Newbie

Joined: Mon Apr 14, 2008 2:01 pm
Posts: 4
It appears that answer to my question is "Yes you're missing something" ;).

If you use Criteria a type is implicitly inferred and by default an instance of Date is mapped as Temporal.TIMESTAMP. So the behaviour is correct and criteria is diferent every time thus not cached.

When using Query "setDate" the "SQL Date" (just day/month/year) is used so caching works. The same effect as with Criteria query is achieved if "setTimestamp" is used instead.


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.