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.
SomeEntity obj1 = session.CreateCriteria(typeof(SomeEntity))
.Add(Restrictions.Eq("Id", new PrimaryKeyId(1)))
.UniqueResult<SomeEntity>();
obj1.SomeValue = 10;
SomeEntity obj2 = session.CreateCriteria(typeof(SomeEntity))
.Add(Restrictions.Eq("Id", new PrimaryKeyId(1)))
.UniqueResult<SomeEntity>();
Assert.AreEqual(8, obj2.SomeValue);
- , 10 obj2, . : unit test. : 2 , ?
- ?
,
edit # 1: NHibernate v2.1.2GA
edit # 2: 2 , .