Thank you again for your answer. I perfetly agree with you.
What I mean is that there is different cache behaivour for entity and
collections.
Consider this example:
Here I've set lazy on collection: <bag name="Bids" inverse="true" cascade=
"all" lazy="true">
<!--Begin Mapping of class Item-->
<class name="Item" lazy="true">
<cache usage="read-write"/>
<id name="Id">
<generator class="assigned" />
</id>
<version name="Version" column="Version" type="Int32" unsaved-value="0"
/>
<property name="Description" />
<bag name="Bids" inverse="true" cascade="all" lazy="true">
<cache usage="read-write" />
<key column="idItem"></key>
<one-to-many class="Bid" />
</bag>
</class>
<!--End Mapping of class Item-->
<!--Begin Mapping of class Bid-->
<class name="Bid" lazy="false">
<cache usage="read-write"/>
<id name="Id" type="Guid">
<generator class="assigned"/>
</id>
<version name="Version" column="Version" type="Int32" unsaved-value="0"
/>
<property name="Description" />
<many-to-one name="Item" column="idItem" class="Item" />
</class>
<!--End Mapping of class Bid-->
</hibernate-mapping>
Guid item_guid = Guid.NewGuid();
Guid bid1_guid = Guid.NewGuid();
Guid bid2_guid = Guid.NewGuid();
using (ISession session = OpenSession())
{
using (var tx = session.BeginTransaction())
{
// item
Item item = new Item();
item.Id = item_guid;
item.Description = "ItemNote";
// bid1
Bid bid1 = new Bid();
bid1.Id = bid1_guid;
bid1.Description = "Bid1Description";
bid1.Item = item;
// bid2
Bid bid2 = new Bid();
bid2.Id = bid2_guid;
bid2.Description = "Bid2Description";
bid2.Item = item;
item.AddBid(bid1);
item.AddBid(bid2);
session.Save(item);
tx.Commit();
}
}
*HERE I'VE 3 INSERT QUERY*
using (ISession session = OpenSession())
{
using (var tx = session.BeginTransaction())
{
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
{
session.Get<Item>(item_guid);
session.Get<Bid>(bid1_guid);
session.Get<Bid>(bid2_guid);
tx.Commit();
}
}
}
NO SELECT QUERY
using (ISession session = OpenSession())
{
using (var tx = session.BeginTransaction())
{
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
{
session.Get<Item>(item_guid);
session.Get<Bid>(bid1_guid);
session.Get<Bid>(bid2_guid);
tx.Commit();
}
}
}
ONE SELECT QUERY
If I compare with previous result this example, I can see that have query
only when need to fetch collection index.
Thank you
Maurizio
Il giorno venerdì 27 novembre 2015 22:56:57 UTC+1, Gunnar Liljas ha scritto:
>
> using (var tx = session.BeginTransaction())
> {
> session.Get<Item>(item_guid); * /* NO QUERY ON DB*/*
>
>
> *Since this object is in 1st level cache, not in 2nd level.*
>
> tx.Commit();
> }
> }
> using (var session = sessionFactory.OpenSession())
> {
> using (var tx = session.BeginTransaction())
> {
> session.Get<Item>(item_guid); */* 1 SELECT QUERY ON
> DB NOT EXPECTED*/*
>
> *I think it is expected. This is the first time this object is fetched
> from DB*
>
> }
> }
> using (var session = sessionFactory.OpenSession())
> {
> using (var tx = session.BeginTransaction())
> {
> session.Get<Item>(item_guid); * /* NO QUERY ON DB*/*
>
> *Second time. It's now in the cache.*
>
> }
> }
>
> 2015-11-27 21:26 GMT+01:00 Mau80 <[email protected] <javascript:>>:
>
>> Thank you for your answer.
>>
>> My consideration is based on the fact that entities (in example both Item
>> and Bid) are retrieved from cache after insert.
>> For collection NHibernate perform a query on db for get collection index
>> (from example Model.Core.Item.Bids#60b8b2d6-4e60-489e-bd08-dce224941b6f),
>> the entities contained into collection are retrieved form cache.
>>
>> So only the collection key is not retrieved from cache but from database.
>> I'm expecting that also collection key is retrieved from cache.
>>
>> Another consideration is that I'm performing a Save operation so entities
>> should be inserted (NHibernate doesn’t perform a query before insert for
>> ensure that entities are not already present in db)
>>
>> Thank you
>> Maurizio
>>
>>
>> Il giorno venerdì 27 novembre 2015 08:59:07 UTC+1, Gunnar Liljas ha
>> scritto:
>>>
>>> I think this is by design, to ensure that the cache is in sync with the
>>> actual data in the database.
>>>
>>> /G
>>>
>>> 2015-11-26 22:17 GMT+01:00 Mau80 <[email protected]>:
>>>
>>>> I've inserted following issue:
>>>>
>>>> https://nhibernate.jira.com/browse/NH-3837
>>>>
>>>>
>>>> Il giorno mercoledì 28 ottobre 2015 16:58:45 UTC+1, Mau80 ha scritto:
>>>>>
>>>>> The problem appears only when I perform insert. Future get works as
>>>>> expected.
>>>>> I'd like to avoid to fetch data from db after have inserted.
>>>>>
>>>>> using (var session = sessionFactory.OpenSession())
>>>>> {
>>>>> using (var tx = session.BeginTransaction())
>>>>> {
>>>>> // item
>>>>> Item item = new Item();
>>>>> item.Id = item_guid;
>>>>> item.ItemDescription = "ItemNote";
>>>>>
>>>>> // bid1
>>>>> Bid bid1 = new Bid();
>>>>> bid1.Id = bid1_guid;
>>>>> bid1.Description = "Bid1Description";
>>>>> bid1.Item = item;
>>>>> // bid2
>>>>> Bid bid2 = new Bid();
>>>>> bid2.Id = bid2_guid;
>>>>> bid2.Description = "Bid2Description";
>>>>> bid2.Item = item;
>>>>>
>>>>> session.Save(item);
>>>>> tx.Commit(); */* 3 INSERT QUERY ON DB */*
>>>>>
>>>>> }
>>>>> using (var tx = session.BeginTransaction())
>>>>> {
>>>>> session.Get<Item>(item_guid); * /* NO QUERY ON
>>>>> DB*/*
>>>>> tx.Commit();
>>>>> }
>>>>> }
>>>>> using (var session = sessionFactory.OpenSession())
>>>>> {
>>>>> using (var tx = session.BeginTransaction())
>>>>> {
>>>>> session.Get<Item>(item_guid); */* 1 SELECT QUERY
>>>>> ON DB NOT EXPECTED*/*
>>>>> }
>>>>> }
>>>>> using (var session = sessionFactory.OpenSession())
>>>>> {
>>>>> using (var tx = session.BeginTransaction())
>>>>> {
>>>>> session.Get<Item>(item_guid); */* NO QUERY ON
>>>>> DB*/*
>>>>> }
>>>>> }
>>>>>
>>>>> Thank you
>>>>> Maurizio
>>>>>
>>>>> Il giorno martedì 27 ottobre 2015 17:41:46 UTC+1, Mau80 ha scritto:
>>>>>>
>>>>>> Hi to all,
>>>>>>
>>>>>> I am having a problem with NHibernate and 2nd Level Cache trying to
>>>>>> retrieve collections.
>>>>>>
>>>>>> In particular I get a cache miss with collection property (no items
>>>>>> contained in collection). Eg:
>>>>>>
>>>>>> Item have a bag of Bid
>>>>>>
>>>>>> In my Item entity, I've following property:
>>>>>>
>>>>>> public IList<Bid> Bids { get; set; }
>>>>>>
>>>>>> I get cache miss on following (note Model.Core.Item.Bids)
>>>>>>
>>>>>> *Model.Core.Item.Bids*#60b8b2d6-4e60-489e-bd08-dce224941b6f
>>>>>>
>>>>>> This is my version of NHibernate:
>>>>>>
>>>>>> NHibernate 4.0.4 GA
>>>>>> NHibernate.Caches.SysCache.dll 4.0.0.4000
>>>>>>
>>>>>> Following my mapping
>>>>>>
>>>>>> <?xml version="1.0" encoding="utf-8" ?>
>>>>>>> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
>>>>>>> assembly="Model.Core"
>>>>>>> namespace="Model.Core">
>>>>>>> <class name="*Item*" lazy="false">
>>>>>>> <cache include="all" usage="read-write"/>
>>>>>>> <id name="Id" type="Guid"
>>>>>>> unsaved-value="00000000-0000-0000-0000-000000000000">
>>>>>>> <generator class="assigned" />
>>>>>>> </id>
>>>>>>> <property name="ItemDescription" />
>>>>>>> <bag name="Bids" lazy="false" inverse="true" cascade="all">
>>>>>>> <cache usage="read-write"></cache>
>>>>>>> <key column="idItem"></key>
>>>>>>> <one-to-many class="Bid" />
>>>>>>> </bag>
>>>>>>> </class>
>>>>>>> </hibernate-mapping>
>>>>>>
>>>>>>
>>>>>> <?xml version="1.0" encoding="utf-8" ?>
>>>>>>> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
>>>>>>> assembly="Model.Core"
>>>>>>> namespace="Model.Core">
>>>>>>> <class name="*Bid*" lazy="false">
>>>>>>> <cache usage="read-only"/>
>>>>>>> <id name="Id" type="Guid">
>>>>>>> <generator class="assigned"/>
>>>>>>> </id>
>>>>>>> <property name="Description" />
>>>>>>> <many-to-one name="Item" column="idItem" class="Item" lazy
>>>>>>> ="false"/>
>>>>>>> <set name="BidDetails" lazy="false" fetch="join" inverse="true"
>>>>>>> access="property">
>>>>>>> <cache usage="read-write"/>
>>>>>>> <key column="idBid"></key>
>>>>>>> <one-to-many class="BidDetail" />
>>>>>>> </set>
>>>>>>> </class>
>>>>>>> </hibernate-mapping>
>>>>>>
>>>>>>
>>>>>>
>>>>>> I can provide more info or code sample.
>>>>>>
>>>>>> Thank you very much
>>>>>> Maurizio
>>>>>>
>>>>>>
>>>>>> --
>>>> 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.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> 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] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/nhusers.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
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.
For more options, visit https://groups.google.com/d/optout.