-->
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.  [ 3 posts ] 
Author Message
 Post subject: ICriteria does not use Session Cache ?
PostPosted: Sat Sep 29, 2007 9:50 am 
Beginner
Beginner

Joined: Fri Jul 29, 2005 10:34 am
Posts: 25
Hibernate version: 1.2

I can't find anything that explains the relation between ICriteria queries and the session cache.
I've this small example below where I've expected only 1 SELECT statement sent to the database.
I am getting 2 SELECT statements (sql profitler) and that's my problem/question.

As you can see, the session "contains" the retrieved object but when the query is executed again, it look like it's not looking inside the session cache.

Code:
   
  ISession session = SessionFactory.CurrentSession;

   ICriteria criteria = session.CreateCriteria(typeof(Category));
   //criteria.SetCacheable(true);

   criteria.Add(new EqExpression("CategoryID", 1));
   Category category = (Category)criteria.UniqueResult();
   Assert.IsTrue(session.Contains(category));

    // fetch it again .
    Category category2 = (Category)criteria.UniqueResult();
    Assert.AreEqual(category, category2);


Note: I've tried to use SetCacheable but i think that's something completelty different than the session cache and it's related to the query cache, which it's not this case.

Note2 : If i replace criteria.UniqueResult() with session.Load() , i get only 1 SELECT (which is what i want but still using ICriteria)

Is this expected behaviour?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 8:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
It may seem like you are getting a single entity through your "criteria" object, thus, the cached Category object should be returned. As far as NHibernate is concerned, "criteria" is a query, and query results are not cached in the ISession.

SetCacheable() relates to second-level cache. See here for a brief explanation of the happenings when caching queries:
http://forum.hibernate.org/viewtopic.ph ... 65#2357765

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 11:00 am 
Beginner
Beginner

Joined: Fri Jul 29, 2005 10:34 am
Posts: 25
First of all, thanks for your reply.

karlchu wrote:
It may seem like you are getting a single entity through your "criteria" object, thus, the cached Category object should be returned.

Yes, actually the cached object is returned since the objects are the same.

karlchu wrote:
As far as NHibernate is concerned, "criteria" is a query, and query results are not cached in the ISession.

Here is where I got confused. I thought that the ICriteria UniqueResult was a kind of session.Load() , and it's not.

Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.