Skip to content

Commit bd16130

Browse files
committed
Port Hibernate's lazy attribute fetch groups
1 parent cbea12e commit bd16130

File tree

65 files changed

+1333
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1333
-160
lines changed

src/NHibernate.DomainModel/Async/CustomPersister.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ public async Task<object> LoadAsync(object id, object optionalObject, LockMode l
8585
if (obj != null)
8686
{
8787
clone = (Custom)obj.Clone();
88-
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, false,
89-
session);
90-
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, false, session);
88+
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, session);
89+
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, session);
9190
await (TwoPhaseLoad.InitializeEntityAsync(clone, false, session, new PreLoadEvent((IEventSource) session),
9291
new PostLoadEvent((IEventSource) session), cancellationToken));
9392
}

src/NHibernate.DomainModel/CustomPersister.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,8 @@ public object Load(object id, object optionalObject, LockMode lockMode, ISession
310310
if (obj != null)
311311
{
312312
clone = (Custom)obj.Clone();
313-
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, false,
314-
session);
315-
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, false, session);
313+
TwoPhaseLoad.AddUninitializedEntity(session.GenerateEntityKey(id, this), clone, this, LockMode.None, session);
314+
TwoPhaseLoad.PostHydrate(this, id, new String[] {obj.Name}, null, clone, LockMode.None, session);
316315
TwoPhaseLoad.InitializeEntity(clone, false, session, new PreLoadEvent((IEventSource) session),
317316
new PostLoadEvent((IEventSource) session));
318317
}

src/NHibernate.Test/Async/CacheTest/SerializationFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ private CacheEntry CreateCacheEntry()
142142
{
143143
DisassembledState = GetAllKnownTypeValues(),
144144
Version = 55,
145-
Subclass = "Test",
146-
AreLazyPropertiesUnfetched = true
145+
Subclass = "Test"
147146
};
148147
}
149148

@@ -240,7 +239,6 @@ private void CheckCacheEntry(CacheEntry original, CacheEntry copy)
240239
Assert.That(copy.Version, Is.EqualTo(original.Version));
241240
Assert.That(copy.Version, Is.TypeOf(original.Version.GetType()));
242241
Assert.That(copy.Subclass, Is.EqualTo(original.Subclass));
243-
Assert.That(copy.AreLazyPropertiesUnfetched, Is.EqualTo(original.AreLazyPropertiesUnfetched));
244242
for (var i = 0; i < copy.DisassembledState.Length; i++)
245243
{
246244
Assert.That(copy.DisassembledState[i], Is.TypeOf(original.DisassembledState[i].GetType()));
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using System.Text;
14+
using System.Threading.Tasks;
15+
using NHibernate.Cache;
16+
using NHibernate.Cfg;
17+
using NUnit.Framework;
18+
19+
namespace NHibernate.Test.LazyGroup
20+
{
21+
using System.Threading;
22+
[TestFixture]
23+
public class LazyGroupFixtureAsync : TestCase
24+
{
25+
protected override string MappingsAssembly => "NHibernate.Test";
26+
27+
protected override string[] Mappings => new[] { "LazyGroup.Mappings.hbm.xml" };
28+
29+
protected override void Configure(Configuration configuration)
30+
{
31+
base.Configure(configuration);
32+
configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName;
33+
configuration.Properties[Environment.UseSecondLevelCache] = "true";
34+
configuration.Properties[Environment.GenerateStatistics] = "true";
35+
}
36+
37+
protected override void OnSetUp()
38+
{
39+
using (var s = OpenSession())
40+
using (var tx = s.BeginTransaction())
41+
{
42+
for (var i = 1; i <= 5; i++)
43+
{
44+
var person = GeneratePerson(i);
45+
s.Save(person);
46+
}
47+
48+
tx.Commit();
49+
}
50+
}
51+
52+
protected override void OnTearDown()
53+
{
54+
using (var s = OpenSession())
55+
using (var tx = s.BeginTransaction())
56+
{
57+
s.CreateQuery("delete from Person").ExecuteUpdate();
58+
tx.Commit();
59+
}
60+
}
61+
62+
[Test]
63+
public async Task TestGroupsAsync()
64+
{
65+
using (var s = OpenSession())
66+
{
67+
var person = await (s.GetAsync<Person>(1));
68+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Name"), Is.True);
69+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.False);
70+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
71+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
72+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
73+
74+
var nickName = person.NickName;
75+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True);
76+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
77+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
78+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
79+
Assert.That(nickName, Is.EqualTo("NickName1"));
80+
81+
var address = person.Address;
82+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.True);
83+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
84+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
85+
Assert.That(address.City, Is.EqualTo("City1"));
86+
Assert.That(address.Street, Is.EqualTo("Street1"));
87+
Assert.That(address.PostCode, Is.EqualTo(1001));
88+
89+
var image = person.Image;
90+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.True);
91+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
92+
Assert.That(person.Image, Has.Length.EqualTo(1));
93+
94+
var age = person.Age;
95+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.True);
96+
Assert.That(person.Age, Is.EqualTo(1));
97+
}
98+
}
99+
100+
[TestCase(true)]
101+
[TestCase(false)]
102+
public async Task TestUpdateAsync(bool fetchBeforeUpdate, CancellationToken cancellationToken = default(CancellationToken))
103+
{
104+
Sfi.Statistics.Clear();
105+
106+
using (var s = OpenSession())
107+
using (var tx = s.BeginTransaction())
108+
{
109+
var person = await (s.GetAsync<Person>(1, cancellationToken));
110+
if (fetchBeforeUpdate)
111+
{
112+
var nickName = person.NickName;
113+
}
114+
115+
person.NickName = "test";
116+
117+
await (tx.CommitAsync(cancellationToken));
118+
}
119+
120+
Assert.That(Sfi.Statistics.EntityUpdateCount, Is.EqualTo(1));
121+
122+
using (var s = OpenSession())
123+
using (var tx = s.BeginTransaction())
124+
{
125+
var person = await (s.GetAsync<Person>(1, cancellationToken));
126+
Assert.That(person.NickName, Is.EqualTo("test"));
127+
128+
person.NickName = "NickName1"; // reset name
129+
130+
await (tx.CommitAsync(cancellationToken));
131+
}
132+
}
133+
134+
[Test]
135+
public async Task TestCacheAsync()
136+
{
137+
var persister = Sfi.GetEntityPersister(typeof(Person).FullName);
138+
var cache = (HashtableCache) persister.Cache.Cache;
139+
await (cache.ClearAsync(CancellationToken.None));
140+
141+
using (var s = OpenSession())
142+
using (var tx = s.BeginTransaction())
143+
{
144+
var person = await (s.GetAsync<Person>(1));
145+
146+
var nickName = person.NickName;
147+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True);
148+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
149+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
150+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
151+
Assert.That(nickName, Is.EqualTo("NickName1"));
152+
153+
await (tx.CommitAsync());
154+
}
155+
156+
using (var s = OpenSession())
157+
using (var tx = s.BeginTransaction())
158+
{
159+
var person = await (s.GetAsync<Person>(1));
160+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.True);
161+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
162+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.False);
163+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
164+
Assert.That(person.NickName, Is.EqualTo("NickName1"));
165+
166+
await (tx.CommitAsync());
167+
}
168+
}
169+
170+
[Test]
171+
public async Task TestInitializeFromCacheAsync()
172+
{
173+
var persister = Sfi.GetEntityPersister(typeof(Person).FullName);
174+
var cache = (HashtableCache) persister.Cache.Cache;
175+
await (cache.ClearAsync(CancellationToken.None));
176+
Sfi.Statistics.Clear();
177+
178+
using (var s = OpenSession())
179+
using (var tx = s.BeginTransaction())
180+
{
181+
var person = await (s.GetAsync<Person>(1));
182+
183+
await (InitializeImageAsync());
184+
185+
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(2));
186+
187+
var image = person.Image; // Should be initialized from cache
188+
189+
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(2));
190+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "NickName"), Is.False);
191+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Address"), Is.False);
192+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Image"), Is.True);
193+
Assert.That(NHibernateUtil.IsPropertyInitialized(person, "Age"), Is.False);
194+
Assert.That(image, Has.Length.EqualTo(1));
195+
196+
await (tx.CommitAsync());
197+
}
198+
}
199+
200+
private async Task InitializeImageAsync(CancellationToken cancellationToken = default(CancellationToken))
201+
{
202+
using (var s = OpenSession())
203+
using (var tx = s.BeginTransaction())
204+
{
205+
var person = await (s.GetAsync<Person>(1, cancellationToken));
206+
var image = person.Image;
207+
208+
await (tx.CommitAsync(cancellationToken));
209+
}
210+
}
211+
212+
private static Person GeneratePerson(int i)
213+
{
214+
return new Person
215+
{
216+
Id = i,
217+
Name = $"Person{i}",
218+
Address = new Address
219+
{
220+
City = $"City{i}",
221+
PostCode = 1000+i,
222+
Street = $"Street{i}"
223+
},
224+
Image = new byte[i],
225+
NickName = $"NickName{i}"
226+
};
227+
228+
}
229+
}
230+
}

src/NHibernate.Test/CacheTest/SerializationFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ private CacheEntry CreateCacheEntry()
131131
{
132132
DisassembledState = GetAllKnownTypeValues(),
133133
Version = 55,
134-
Subclass = "Test",
135-
AreLazyPropertiesUnfetched = true
134+
Subclass = "Test"
136135
};
137136
}
138137

@@ -229,7 +228,6 @@ private void CheckCacheEntry(CacheEntry original, CacheEntry copy)
229228
Assert.That(copy.Version, Is.EqualTo(original.Version));
230229
Assert.That(copy.Version, Is.TypeOf(original.Version.GetType()));
231230
Assert.That(copy.Subclass, Is.EqualTo(original.Subclass));
232-
Assert.That(copy.AreLazyPropertiesUnfetched, Is.EqualTo(original.AreLazyPropertiesUnfetched));
233231
for (var i = 0; i < copy.DisassembledState.Length; i++)
234232
{
235233
Assert.That(copy.DisassembledState[i], Is.TypeOf(original.DisassembledState[i].GetType()));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace NHibernate.Test.LazyGroup
8+
{
9+
public class Address
10+
{
11+
public string City { get; set; }
12+
13+
public string Street { get; set; }
14+
15+
public int PostCode { get; set; }
16+
}
17+
}

0 commit comments

Comments
 (0)