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.  [ 5 posts ] 
Author Message
 Post subject: know if the object is in cache or not using PK
PostPosted: Tue Sep 16, 2008 10:34 am 
Newbie

Joined: Mon Feb 11, 2008 2:45 am
Posts: 5
Location: Bangalore
There were many situations when I had to make sure that I Get (or Load) fresh entity object from database and not from cache.
What I believe is with NHibernate there is no other way to do so except to use UnitOfWork.Refresh method after Get. This may do 2 database requests if object was not into the cache.

Otherway I can put it as, I need to know if there is anything by which could know if the object is in cache or not using Primary key

I don't want to change cache control setting which may affect current performance of my software.

Thanks in advance,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 7:19 am 
Newbie

Joined: Mon Feb 11, 2008 2:45 am
Posts: 5
Location: Bangalore
What I need is something like this,

Code:
UnitOfWork.GetFresh(object pk)


OR
Code:
if(UnitOfWork.IsCached(object pk) && !UnitOfWork.IsDirty(object pk)
{
      //Refresh
}
else
{
     //Get
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 9:26 am 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
You can use Evict() to remove object and its collections for the first level cache. You can also use Contains() on ISession to see if instance belongs to a session cache. See chapter 15.3. in NH reference (Managing caches)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 9:53 am 
Newbie

Joined: Mon Feb 11, 2008 2:45 am
Posts: 5
Location: Bangalore
But "Contains" and "Evict" both needs objects with me.. I cannot evict by Primary key and so, cannot avoid 2 DB hit.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 10:00 am 
Newbie

Joined: Mon Feb 11, 2008 2:45 am
Posts: 5
Location: Bangalore
ok.. I guess if I create object using 'new' and then assign primary key to it and then try to see if Session 'Contains' the object.
My entity object should implement 'Equals' or overrride hash code generation...

Thanks exp2000


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