No, I'm not meaning first-level cache entities.
If we take the example from ayende's blog we have the following code.
using (var s = sf.OpenSession())using (var tx = s.BeginTransaction())
{
s.CreateCriteria<User>()
.Add(Restrictions.NaturalId()
.Set("Username", "Ayende"))
.SetCacheable(true)
.UniqueResult();
tx.Commit();
}
This will hit the second level cache for the query and the query is created
and checked agains the StandardQueryCache,
NHibernate.Cache.StandardQueryCache.Get(QueryKey key, ICacheAssembler[]
returnTypes, bool isNaturalKeyLookup, ISet<string> spaces,
ISessionImplementor session) where the isNaturalKeyLookup is true and the
timestamp-check for the result are ignored and the primary key for the
query is directly found without executing the query to the database even if
the timestamp for the query is invalid.
The reason to not do the extra roundtrip to database is to fetch ex user
based on the username (username is unique and can be used as PK) instead of
fetching by PK.
// Patric
On 29 May 2013 18:35, Ricardo Peres <[email protected]> wrote:
> You mean querying first level cache entities?
>
> If so, try this extension method:
>
> public static IEnumerable<T> Local<T>(this ISession session)
> {
> ISessionImplementor impl = session.GetSessionImplementation();
> IPersistenceContext persistence = impl.PersistenceContext;
>
> return (persistence.EntityEntries.Keys.OfType<T>());
> }
>
>
> Use like this:
>
> var c = session.Local<Customer>().Where(x => x.Name == "ricardo").ToList();
>
>
> RP
>
>
>
> On Wednesday, May 29, 2013 5:19:03 PM UTC+1, Patric Forsgard wrote:
>>
>> Hi.
>>
>> Is it possible to get Linq queries transformed on the same way as
>> ICriteria to find entities on natural key without going to database?
>>
>> Regarding to ayende's blog http://ayende.com/blog/**
>> 4061/nhibernate-natural-id<http://ayende.com/blog/4061/nhibernate-natural-id>
>> the
>> query that is generated is the same as using equal. The different I found
>> is when code is asking query-cache the naturalId-parameter is always false
>> for linq-queries.
>>
>> Using NHibernate version 3.3.3.
>>
>> // Patric
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/nhusers?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.