Hi! Caching is a bit more difficult to get right than it seems initially.
All entities you want cached should have the cache tag on them, and this
includes your referenced object mappings.  If you mark the entity you're
referencing as cached, then you should be set for your many-to-one
relationships, i.e. . If your query was for Dog and Dog has one Owner(type
Person) you would need to set the type Person to be cached to not get the db
hit when you traverse the dog-owner relationship. The reason you need to
cache Person is because cached Dog only has the OwnerId in it, not a whole
Person. NH will index objects of type Person by that ID so if you have
Person cached NH will grab it there instead of going to the DB.

Traversing collections off your cached object is similar, but is a bit more
involved. Let's say your Dog had a collection of Puppies.  Sure, you may
have all Dog objects caching, but when it you have NH cache a Dog, it only
caches the things that are in that DB row.  if you think about how
collections are implemented in a database, the relationship to Dog isn't in
that Dog row, it's in the rows of each of the Puppies as a foreign key so
it's not cached with Dog.  Even if each of the puppies is cached, it's that
relationship FROM Dog TO the puppies that you need, and will get the query
hit for.  Don't want that DB hit?  there's an easy fix:  put a cache tag
*inside your collection definitions*, i.e. your lists, sets, bags.  That
will cache the *list of ids* of the puppies that Dog has.  Your puppy
objects themselve are already cached, right? So now it's like the Dog to
Owner: cached id to object, no db hit.

Hope that helps,
Anne




On Fri, Nov 13, 2009 at 3:14 PM, eigeneachse <[email protected]>wrote:

>
> h...@all,
> i have read various posts, blogs.... about the nhibernate caching
> methods. For me it is good that my SysCache2 configuration is working.
> But it is only working in parts and i have a few problems in my
> understanding if this is an configuration failure on my side or if
> there is something other wrong.
>
> So i have the following HQL Query
>
> return Session.CreateQuery(@"
>                select something.....")
>                    .SetParameter(0, aParameter)
>                    .SetParameter(1, aSecondParameter)
>                    .SetCacheable(true)
>                    .SetCacheRegion("10Minutes")
>                    .List<aClass>()[0];
>
> The returning class itself has some references to other tables which
> are configured via an one-to-many relationship. When i call this query
> then the first time the sql is fired against the database. So the base
> objects and the referenced objects are fetched from the database. If i
> do this a second time, then only the referenced objects are fetched
> over sql again. But why? I think i have configured that everything
> should be cached.
> And here is where my understanding in nhibernate caching ends. Are
> only the base objects cached? Why arent the referenced objects cached?
> I read about the query cache and the entity cache. I think that here
> the query cache is used. How can the referenced objects be cached?
>
> It would be very good if someone could help my with this. I am really
> optimistic that this problem is solved already.
> Please let me know if you need more informations.
>
> Thanks in advance for your help
>
> Regards
>
> eigeneachse
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to