From 005aa4468039693d2c88bcd47f35575336d0188f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= <12201973+fredericdelaporte@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:57:36 +0100 Subject: [PATCH 1/2] Fix Clear forgetting the non lazy collections fix #3424 --- .../NHSpecificTest/GH3424/Entities.cs | 18 +++++++ .../NHSpecificTest/GH3424/Fixture.cs | 49 +++++++++++++++++++ .../NHSpecificTest/GH3424/Mappings.hbm.xml | 19 +++++++ .../Engine/StatefulPersistenceContext.cs | 1 + 4 files changed, 87 insertions(+) create mode 100644 src/NHibernate.Test/NHSpecificTest/GH3424/Entities.cs create mode 100644 src/NHibernate.Test/NHSpecificTest/GH3424/Fixture.cs create mode 100644 src/NHibernate.Test/NHSpecificTest/GH3424/Mappings.hbm.xml diff --git a/src/NHibernate.Test/NHSpecificTest/GH3424/Entities.cs b/src/NHibernate.Test/NHSpecificTest/GH3424/Entities.cs new file mode 100644 index 00000000000..e72fe68c43c --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH3424/Entities.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; + +namespace NHibernate.Test.NHSpecificTest.GH3424 +{ + class Entity + { + public virtual Guid Id { get; set; } + public virtual string Name { get; set; } + public virtual ISet Children { get; set; } + } + + class Child + { + public virtual Guid Id { get; set; } + public virtual string Name { get; set; } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH3424/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/GH3424/Fixture.cs new file mode 100644 index 00000000000..5c829ce5c2d --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH3424/Fixture.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using System.Linq; +using NHibernate.Linq; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH3424 +{ + [TestFixture] + public class Fixture : BugTestCase + { + protected override void OnSetUp() + { + using var session = OpenSession(); + using var transaction = session.BeginTransaction(); + + var c1 = new Child { Name = "Rob" }; + session.Save(c1); + var e1 = new Entity { Name = "Bob", Children = new HashSet { c1 } }; + session.Save(e1); + + transaction.Commit(); + } + + protected override void OnTearDown() + { + using var session = OpenSession(); + using var transaction = session.BeginTransaction(); + session.CreateQuery("delete Child").ExecuteUpdate(); + session.CreateQuery("delete from System.Object").ExecuteUpdate(); + + transaction.Commit(); + } + + [Test] + public void QueryingAfterFutureThenClear() + { + using var session = OpenSession(); + using var transaction = session.BeginTransaction(); + var futureBob = session.Query().Where(e => e.Name == "Bob").ToFutureValue(q => q.FirstOrDefault()); + var bob = futureBob.Value; + Assert.That(bob, Is.Not.Null); + session.Clear(); + + var allQuery = session.Query(); + Assert.That(() => allQuery.ToList(), Has.Count.EqualTo(1)); + transaction.Commit(); + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH3424/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/GH3424/Mappings.hbm.xml new file mode 100644 index 00000000000..e692e287c11 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH3424/Mappings.hbm.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/NHibernate/Engine/StatefulPersistenceContext.cs b/src/NHibernate/Engine/StatefulPersistenceContext.cs index 7819a59989b..f912c9b8bdc 100644 --- a/src/NHibernate/Engine/StatefulPersistenceContext.cs +++ b/src/NHibernate/Engine/StatefulPersistenceContext.cs @@ -264,6 +264,7 @@ public void Clear() entityEntries.Clear(); entitySnapshotsByKey.Clear(); collectionsByKey.Clear(); + nonlazyCollections.Clear(); collectionEntries.Clear(); if (unownedCollections != null) { From 5135f15f833dd17a8f48557f704c061aa1015315 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 2 Feb 2024 18:01:01 +0000 Subject: [PATCH 2/2] Generate async files --- .../Async/NHSpecificTest/GH3424/Fixture.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/NHibernate.Test/Async/NHSpecificTest/GH3424/Fixture.cs diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH3424/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH3424/Fixture.cs new file mode 100644 index 00000000000..ef60ff14aac --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH3424/Fixture.cs @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using System.Collections.Generic; +using System.Linq; +using NHibernate.Linq; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH3424 +{ + using System.Threading.Tasks; + [TestFixture] + public class FixtureAsync : BugTestCase + { + protected override void OnSetUp() + { + using var session = OpenSession(); + using var transaction = session.BeginTransaction(); + + var c1 = new Child { Name = "Rob" }; + session.Save(c1); + var e1 = new Entity { Name = "Bob", Children = new HashSet { c1 } }; + session.Save(e1); + + transaction.Commit(); + } + + protected override void OnTearDown() + { + using var session = OpenSession(); + using var transaction = session.BeginTransaction(); + session.CreateQuery("delete Child").ExecuteUpdate(); + session.CreateQuery("delete from System.Object").ExecuteUpdate(); + + transaction.Commit(); + } + + [Test] + public async Task QueryingAfterFutureThenClearAsync() + { + using var session = OpenSession(); + using var transaction = session.BeginTransaction(); + var futureBob = session.Query().Where(e => e.Name == "Bob").ToFutureValue(q => q.FirstOrDefault()); + var bob = await (futureBob.GetValueAsync()); + Assert.That(bob, Is.Not.Null); + session.Clear(); + + var allQuery = session.Query(); + Assert.That(() => allQuery.ToListAsync(), Has.Count.EqualTo(1)); + await (transaction.CommitAsync()); + } + } +}