Note that there are some explanatory texts on larger screens.

plurals
  1. POnHibernate eager loading- strange update behaviour
    primarykey
    data
    text
    <p>my domain is an airport, which contains several terminals, and each terminal contains zones etc.<br> since the number of airport / terminal / zone entities is very small, i'd like to:<br> 1. load all the heirarchy eagerly when retrieving an airport.<br> (using the following fluent configuration:<br></p> <pre><code>//eagerly load terminals mapping.HasMany(x =&gt; x.Terminals).Not.LazyLoad() .Cache.ReadWrite(); </code></pre> <p>)<br> 2. enable 2nd level caching, so that all retrievals of an airport object would not hit the DB.<br> <br> the eager loading and caching works fine, but the following test produces some strange behaviour.<br> (the following code retrieves an airport entity twice (not hitting the DB the second time), and updates one of them.)<br></p> <pre><code> [TestMethod] public void TestSecondLevelCache() { Airport firstAirport = null, secondAirport = null; Console.WriteLine("first select"); using (ISession session = this.SessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { //the idea here is to see whether there are two calls to DB here. check the sql output AirportDAO dao = new AirportDAO(session); firstAirport = dao.GetAirport(); transaction.Commit(); } } Console.WriteLine("second select"); using (ISession session = this.SessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { //the idea here is to see whether there are two calls to DB here. check the sql output AirportDAO dao = new AirportDAO(session); secondAirport = dao.GetAirport(); transaction.Commit(); } } Console.WriteLine("Are those the same airport instance? " + firstAirport.Equals(secondAirport)); Console.WriteLine("now adding a terminal"); using (ISession session = this.SessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { secondAirport.Terminals.Add(new Terminal() { Name = "terminal added to second airport", Zones = new List&lt;Zone&gt;() }); session.Update(secondAirport); transaction.Commit(); } } //this Assert fails, since firstAirport != secondAirport Assert.IsNotNull(firstAirport.Terminals.FirstOrDefault(t =&gt; t.Name.Contains("second airport"))); } </code></pre> <p>see the resulting output:<br></p> <blockquote> <p>first select<br> NHibernate: SELECT airport0_.Id as Id36_0_, airport0_.Name as Name36_0_, airport0_.IsDeleted as IsDeleted36_0_ FROM dbo.[Airport] airport0_ WHERE airport0_.Id=@p0;@p0 = 1<br><br> NHibernate: SELECT terminals0_.Airport_id as Airport4_1_, terminals0_.Id as Id1_, terminals0_.Id as Id50_0_, terminals0_.Name as Name50_0_, terminals0_.IsDeleted as IsDeleted50_0_, terminals0_.Airport_id as Airport4_50_0_ FROM dbo.[Terminal] terminals0_ WHERE terminals0_.Airport_id=@p0;@p0 = 1<br><br> NHibernate: SELECT zones0_.Terminal_id as Terminal4_1_, zones0_.Id as Id1_, zones0_.Id as Id51_0_, zones0_.Name as Name51_0_, zones0_.IsDeleted as IsDeleted51_0_, zones0_.Terminal_id as Terminal4_51_0_ FROM dbo.[Zone] zones0_ WHERE zones0_.Terminal_id=@p0;@p0 = 2<br><br> <br> second select<br> Are those the same airport instance? False<br> <br> now adding a terminal<br> NHibernate: select next_hi from dbo._uniqueKey with (updlock, rowlock)<br> NHibernate: update dbo._uniqueKey set next_hi = @p0 where next_hi = @p1;@p0 = 17, @p1 = 16<br><br> NHibernate: INSERT INTO dbo.[Terminal] (Name, IsDeleted, Airport_id, Id) VALUES (@p0, @p1, @p2, @p3);@p0 = 'terminal added to second airport', @p1 = False, @p2 = NULL, @p3 = 16<br> NHibernate: UPDATE dbo.[Airport] SET Name = @p0, IsDeleted = @p1 WHERE Id = @p2;@p0 = 'test airport', @p1 = False, @p2 = 1<br><br> NHibernate: UPDATE dbo.[Terminal] SET Name = @p0, IsDeleted = @p1, Airport_id = @p2 WHERE Id = @p3;@p0 = 'test terminal', @p1 = False, @p2 = 1, @p3 = 2<br><br> NHibernate: UPDATE dbo.[Zone] SET Name = @p0, IsDeleted = @p1, Terminal_id = @p2 WHERE Id = @p3;@p0 = 'test zone', @p1 = False, @p2 = 2, @p3 = 3<br><br> NHibernate: UPDATE dbo.[Terminal] SET Airport_id = @p0 WHERE Id = @p1;@p0 = 1, @p1 = 16<br> <br><br> </blockquote> <p><br>my problems are:<br> 1. the strange update behaviour which updates everything...<br> 2. the fact that firstAirport and secondAirport are not the same object (maybe I'm missing something about 2nd level cache?)<br><br> thanks in advance,<br> Jhonny</p> </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload