Are NHibernate ICriteria requested or put on an identity card?

Using NHibernate I usually request single entries using the Get () or Load () methods (depending on whether I need a proxy server or not):

SomeEntity obj = session.Get<SomeEntity>(new PrimaryKeyId(1));

Now, if I execute this statement twice, as in the example below, I see only one request running in my unittests:

SomeEntity obj1 = session.Get<SomeEntity>(new PrimaryKeyId(1));
SomeEntity obj2 = session.Get<SomeEntity>(new PrimaryKeyId(1));

So far so good. But I noticed strange behavior when getting the same object using an ICriteria request. See my code below: I get the first instance of the object. Then I change the value of the property to 10 (the value in the database is 8), gets another instance, and finally checks the values ​​of the second instance of the object.

//get the first object instance.
SomeEntity obj1 = session.CreateCriteria(typeof(SomeEntity))
                         .Add(Restrictions.Eq("Id", new PrimaryKeyId(1)))
                         .UniqueResult<SomeEntity>();

//the value in the database and the property is 8 at this point. Let set it to 10.
obj1.SomeValue = 10;

//get the second object instance.
SomeEntity obj2 = session.CreateCriteria(typeof(SomeEntity))
                         .Add(Restrictions.Eq("Id", new PrimaryKeyId(1)))
                         .UniqueResult<SomeEntity>();

//check if the values match.
Assert.AreEqual(8, obj2.SomeValue);

- , 10 obj2, . : unit test. : 2 , ?

- ?

,

edit # 1: NHibernate v2.1.2GA edit # 2: 2 , .

+3
6

, NHibernate, : ICriteria , NHibernate. NHibernate , , . , , , . , .

"-!" : , , 5 , . 5 , . 10 , NHibernate , 5 5 . , 5 ( ).

+2

Get/Load 1- , db. 1- . .

. , 2- . , , 1- , , - . , . . NHibernate.Loader.Loader.GetRow. :

//If the object is already loaded, return the loaded one
obj = session.GetEntityUsingInterceptor(key);
+2

AFAIK, 'Get' (, , Load) 1- .

API , , .

:

+1

, , NHibernate - ID , .

0

NHibernate, , , concurrency. , Get .

, PrimaryKeyId?

EDIT:

( ), . , Evict . Refresh, .

0

In my understanding, when you use a criterion, you basically tell NHibernate: "I want to filter strings based on expressions." When this is visible, NHibernate does not know whether the query will always return the same filtered rows from the database, so it must query it again.

In addition, you can use query caching only with second-level caching, according to the documentation:

Thus, the query cache should always be used in conjunction with the second level cache.

From here

0
source

Source: https://habr.com/ru/post/1788509/


All Articles