Skip to content

Commit d277be2

Browse files
maca88fredericDelaporte
authored andcommitted
Fix lazy property caching
1 parent ce6e8ef commit d277be2

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs

+32
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ protected override string[] Mappings
3636
get { return new[] { "LazyProperty.Mappings.hbm.xml" }; }
3737
}
3838

39+
protected override string CacheConcurrencyStrategy => null;
40+
3941
protected override DebugSessionFactory BuildSessionFactory()
4042
{
4143
using (var logSpy = new LogSpy(typeof(EntityMetamodel)))
@@ -313,5 +315,35 @@ public async Task CanMergeTransientWithLazyPropertyAsync()
313315
Assert.That(book.ALotOfText, Is.EqualTo("a lot of text two..."));
314316
}
315317
}
318+
319+
[Test]
320+
public async Task CacheShouldNotContainLazyPropertiesAsync()
321+
{
322+
Book book;
323+
324+
using (var s = OpenSession())
325+
using (var tx = s.BeginTransaction())
326+
{
327+
328+
book = await (s.CreateQuery("from Book b fetch all properties where b.Id = :id")
329+
.SetParameter("id", 1)
330+
.UniqueResultAsync<Book>());
331+
await (tx.CommitAsync());
332+
}
333+
334+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
335+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.True);
336+
337+
using (var s = OpenSession())
338+
using (var tx = s.BeginTransaction())
339+
{
340+
341+
book = await (s.GetAsync<Book>(1));
342+
await (tx.CommitAsync());
343+
}
344+
345+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
346+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.False);
347+
}
316348
}
317349
}

src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs

+32
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ protected override string[] Mappings
2323
get { return new[] { "LazyProperty.Mappings.hbm.xml" }; }
2424
}
2525

26+
protected override string CacheConcurrencyStrategy => null;
27+
2628
protected override DebugSessionFactory BuildSessionFactory()
2729
{
2830
using (var logSpy = new LogSpy(typeof(EntityMetamodel)))
@@ -306,5 +308,35 @@ public void CanMergeTransientWithLazyProperty()
306308
Assert.That(book.ALotOfText, Is.EqualTo("a lot of text two..."));
307309
}
308310
}
311+
312+
[Test]
313+
public void CacheShouldNotContainLazyProperties()
314+
{
315+
Book book;
316+
317+
using (var s = OpenSession())
318+
using (var tx = s.BeginTransaction())
319+
{
320+
321+
book = s.CreateQuery("from Book b fetch all properties where b.Id = :id")
322+
.SetParameter("id", 1)
323+
.UniqueResult<Book>();
324+
tx.Commit();
325+
}
326+
327+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
328+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.True);
329+
330+
using (var s = OpenSession())
331+
using (var tx = s.BeginTransaction())
332+
{
333+
334+
book = s.Get<Book>(1);
335+
tx.Commit();
336+
}
337+
338+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
339+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.False);
340+
}
309341
}
310342
}

src/NHibernate.Test/LazyProperty/Mappings.hbm.xml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace="NHibernate.Test.LazyProperty">
55

66
<class name="Book">
7+
<cache usage="nonstrict-read-write" include="non-lazy" />
78
<id name="Id">
89
<generator class="assigned" />
910
</id>

src/NHibernate/Async/Cache/Entry/CacheEntry.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public static async Task<CacheEntry> CreateAsync(object[] state, IEntityPersiste
2929
return new CacheEntry
3030
{
3131
//disassembled state gets put in a new array (we write to cache by value!)
32-
DisassembledState = await (TypeHelper.DisassembleAsync(state, persister.PropertyTypes, null, session, owner, cancellationToken)).ConfigureAwait(false),
32+
DisassembledState = await (TypeHelper.DisassembleAsync(
33+
state,
34+
persister.PropertyTypes,
35+
persister.IsLazyPropertiesCacheable ? null : persister.PropertyLaziness,
36+
session,
37+
owner, cancellationToken)).ConfigureAwait(false),
3338
AreLazyPropertiesUnfetched = unfetched || !persister.IsLazyPropertiesCacheable,
3439
Subclass = persister.EntityName,
3540
Version = version

src/NHibernate/Cache/Entry/CacheEntry.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public static CacheEntry Create(object[] state, IEntityPersister persister, bool
4040
return new CacheEntry
4141
{
4242
//disassembled state gets put in a new array (we write to cache by value!)
43-
DisassembledState = TypeHelper.Disassemble(state, persister.PropertyTypes, null, session, owner),
43+
DisassembledState = TypeHelper.Disassemble(
44+
state,
45+
persister.PropertyTypes,
46+
persister.IsLazyPropertiesCacheable ? null : persister.PropertyLaziness,
47+
session,
48+
owner),
4449
AreLazyPropertiesUnfetched = unfetched || !persister.IsLazyPropertiesCacheable,
4550
Subclass = persister.EntityName,
4651
Version = version

src/NHibernate/Cfg/XmlHbmBinding/RootClassBinder.cs

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ private static void BindCache(HbmCache cacheSchema, RootClass rootClass)
268268
{
269269
rootClass.CacheConcurrencyStrategy = cacheSchema.usage.ToCacheConcurrencyStrategy();
270270
rootClass.CacheRegionName = cacheSchema.region;
271+
rootClass.SetLazyPropertiesCacheable(cacheSchema.include == HbmCacheInclude.All);
271272
}
272273
}
273274
}

0 commit comments

Comments
 (0)