diff --git a/src/NHibernate.Test/NHSpecificTest/NH750/Device.cs b/src/NHibernate.Test/NHSpecificTest/NH750/Device.cs index 897045acaf6..b4496828089 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH750/Device.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH750/Device.cs @@ -6,6 +6,11 @@ namespace NHibernate.Test.NHSpecificTest.NH750 { public class Device { + private int _id; + private Device _template; + private IList _drives = new List(); + private IList _drivesNotIgnored = new List(); + public Device() : base() { } @@ -16,9 +21,13 @@ public Device(string manifacturer) _manifacturer = manifacturer; } - private int _id; + public virtual Device Template + { + get => _template; + set => _template = value; + } - public int Id + public virtual int Id { get { return _id; } set { _id = value; } @@ -26,22 +35,20 @@ public int Id private string _manifacturer; - public string Manifacturer + public virtual string Manifacturer { get { return _manifacturer; } set { _manifacturer = value; } } - private IList _drives = new List(); - private IList _drivesNotIgnored = new List(); - public IList Drives + public virtual IList Drives { get { return _drives; } set { _drives = value; } } - public IList DrivesNotIgnored + public virtual IList DrivesNotIgnored { get => _drivesNotIgnored; set => _drivesNotIgnored = value; diff --git a/src/NHibernate.Test/NHSpecificTest/NH750/Drive.cs b/src/NHibernate.Test/NHSpecificTest/NH750/Drive.cs index b449dfb27ea..0e473bdf875 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH750/Drive.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH750/Drive.cs @@ -16,7 +16,7 @@ public Drive(string classFullName) private int _id; - public int Id + public virtual int Id { get { return _id; } set { _id = value; } @@ -24,7 +24,7 @@ public int Id private string _classFullName; - public string ClassFullName + public virtual string ClassFullName { get { return _classFullName; } set { _classFullName = value; } @@ -44,4 +44,4 @@ public override int GetHashCode() return _classFullName.GetHashCode(); } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyFixture.cs b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyFixture.cs new file mode 100644 index 00000000000..9aa01aa0389 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyFixture.cs @@ -0,0 +1,144 @@ +using System; +using NHibernate.Criterion; +using NHibernate.Transform; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH750 +{ + [TestFixture(0)] + [TestFixture(1)] + [TestFixture(2)] + public class ManyToManyFixture : BugTestCase + { + private int id2; + private readonly int _drivesCount; + private int _withTempalteId; + private int ValidDrivesCount => _drivesCount; + + public ManyToManyFixture(int drivesCount) + { + _drivesCount = drivesCount; + } + + protected override void OnSetUp() + { + Drive dr1 = new Drive("Drive 1"); + Drive dr2 = new Drive("Drive 2"); + Drive dr3 = new Drive("Drive 3"); + Device dv1 = new Device("Device 1"); + Device dv2 = new Device("Device 2"); + var withTempalte = new Device("Device With Device 2 template") { Template = dv2 }; + using var s = Sfi.OpenSession(); + using var t = s.BeginTransaction(); + s.Save(dr1); + s.Save(dr2); + s.Save(dr3); + AddDrive(dv1, dr2); + AddDrive(dv1, dr1); + AddDrive(dv2, dr3); + AddDrive(dv2, dr1); + + s.Save(dv1); + id2 = (int) s.Save(dv2); + _withTempalteId = (int)s.Save(withTempalte); + t.Commit(); + } + + private void AddDrive(Device dv, Drive drive) + { + if(dv.DrivesNotIgnored.Count >= _drivesCount) + return; + dv.DrivesNotIgnored.Add(drive); + } + + protected override void OnTearDown() + { + using var s = Sfi.OpenSession(); + using var t = s.BeginTransaction(); + + s.CreateSQLQuery("delete from DriveOfDevice").ExecuteUpdate(); + s.Delete("from Device"); + s.Delete("from Drive"); + t.Commit(); + } + + [Test] + public void QueryOverFetch() + { + using var log = new SqlLogSpy(); + using var s = OpenSession(); + var dv2 = s.QueryOver() + .Fetch(SelectMode.Fetch, x => x.DrivesNotIgnored) + .Where(Restrictions.IdEq(id2)) + .TransformUsing(Transformers.DistinctRootEntity) + .SingleOrDefault(); + + Assert.That(NHibernateUtil.IsInitialized(dv2.DrivesNotIgnored), Is.True); + Assert.That(dv2.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); + } + + [Test] + public void QueryOverFetch2() + { + using var log = new SqlLogSpy(); + using var s = OpenSession(); + var withTemplate = s.QueryOver() + .Fetch(SelectMode.Fetch, x => x.Template, x => x.Template.DrivesNotIgnored) + .Where(Restrictions.IdEq(_withTempalteId)) + .TransformUsing(Transformers.DistinctRootEntity) + .SingleOrDefault(); + + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True); + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.DrivesNotIgnored), Is.True); + Assert.That(withTemplate.Template.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); + } + + [Test] + public void HqlFetch() + { + using var log = new SqlLogSpy(); + using var s = OpenSession(); + var dv2 = s.CreateQuery("from Device d left join fetch d.DrivesNotIgnored where d.id = :id") + .SetResultTransformer(Transformers.DistinctRootEntity) + .SetParameter("id", id2) + .UniqueResult(); + + Assert.That(NHibernateUtil.IsInitialized(dv2.DrivesNotIgnored), Is.True); + Assert.That(dv2.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); + } + + [Test] + public void HqlFetch2() + { + using var log = new SqlLogSpy(); + using var s = OpenSession(); + var withTemplate = s.CreateQuery("from Device t left join fetch t.Template d left join fetch d.DrivesNotIgnored where d.id = :id") + .SetResultTransformer(Transformers.DistinctRootEntity) + .SetParameter("id", id2) + .UniqueResult(); + + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True); + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.DrivesNotIgnored), Is.True); + Assert.That(withTemplate.Template.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); + } + + [Test] + public void LazyLoad() + { + using var log = new SqlLogSpy(); + using var s = OpenSession(); + + var dv2 = s.Get(id2); + + NHibernateUtil.Initialize(dv2.DrivesNotIgnored); + Assert.That(NHibernateUtil.IsInitialized(dv2.DrivesNotIgnored), Is.True); + Assert.That(dv2.DrivesNotIgnored, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); + // First query for Device, second for Drives collection + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(2)); + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs index 3cb8a16ed31..fa95e169513 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyNotFoundIgnoreFixture.cs @@ -13,6 +13,7 @@ public class ManyToManyNotFoundIgnoreFixture : BugTestCase private int id1; private int id2; private int _drive2Id; + private int _withTempalteId; private readonly int _drivesCount; private int ValidDrivesCount => _drivesCount == 0? 0 : _drivesCount - 1; @@ -28,25 +29,26 @@ protected override void OnSetUp() Drive dr3 = new Drive("Drive 3"); Device dv1 = new Device("Device 1"); Device dv2 = new Device("Device 2"); - using (var s = Sfi.OpenSession()) - using (var t = s.BeginTransaction()) - { - s.Save(dr1); - _drive2Id = (int)s.Save(dr2); - s.Save(dr3); - AddDrive(dv1, dr2); - AddDrive(dv1, dr1); - AddDrive(dv2, dr3); - AddDrive(dv2, dr1); - - id1 = (int) s.Save(dv1); - id2 = (int) s.Save(dv2); - s.Flush(); + var withTempalte = new Device("Device With Device 2 template") { Template = dv2 }; - s.Clear(); - s.Delete(dr3); - t.Commit(); - } + using var s = Sfi.OpenSession(); + using var t = s.BeginTransaction(); + s.Save(dr1); + _drive2Id = (int)s.Save(dr2); + s.Save(dr3); + AddDrive(dv1, dr2); + AddDrive(dv1, dr1); + AddDrive(dv2, dr3); + AddDrive(dv2, dr1); + + id1 = (int) s.Save(dv1); + id2 = (int) s.Save(dv2); + _withTempalteId = (int)s.Save(withTempalte); + s.Flush(); + + s.Clear(); + s.Delete(dr3); + t.Commit(); } private void AddDrive(Device dv, Drive drive) @@ -76,6 +78,8 @@ public void DeviceOfDrive() { dv1 = (Device) s.Load(typeof(Device), id1); dv2 = (Device) s.Load(typeof(Device), id2); + NHibernateUtil.Initialize(dv1.Drives); + NHibernateUtil.Initialize(dv2.Drives); } Assert.That(dv1.Drives, Has.Count.EqualTo(_drivesCount).And.None.Null); @@ -141,6 +145,23 @@ public void QueryOverFetch() Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); } + [Test] + public void QueryOverFetch2() + { + using var log = new SqlLogSpy(); + using var s = OpenSession(); + var withTemplate = s.QueryOver() + .Fetch(SelectMode.Fetch, x => x.Template, x => x.Template.Drives) + .Where(Restrictions.IdEq(_withTempalteId)) + .TransformUsing(Transformers.DistinctRootEntity) + .SingleOrDefault(); + + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True); + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.Drives), Is.True); + Assert.That(withTemplate.Template.Drives, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); + } + [Test] public void HqlFetch() { @@ -157,17 +178,31 @@ public void HqlFetch() } [Test] - public void LazyLoad() + public void HqlFetch2() { using var log = new SqlLogSpy(); using var s = OpenSession(); + var withTemplate = s.CreateQuery("from Device t left join fetch t.Template d left join fetch d.Drives where d.id = :id") + .SetResultTransformer(Transformers.DistinctRootEntity) + .SetParameter("id", id2) + .UniqueResult(); + + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template), Is.True); + Assert.That(NHibernateUtil.IsInitialized(withTemplate.Template.Drives), Is.True); + Assert.That(withTemplate.Template.Drives, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); + } + + [Test] + public void LazyLoad() + { + using var s = OpenSession(); var dv2 = s.Get(id2); + using var log = new SqlLogSpy(); - Assert.That(NHibernateUtil.IsInitialized(dv2.Drives), Is.True); Assert.That(dv2.Drives, Has.Count.EqualTo(ValidDrivesCount).And.None.Null); - // First query for Device, second for Drives collection - Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(2)); + Assert.That(log.Appender.GetEvents().Length, Is.EqualTo(1)); } } } diff --git a/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyThrowsForNotFoundFixture.cs b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyThrowsForNotFoundFixture.cs index bef312c398c..10602bbee7f 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyThrowsForNotFoundFixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH750/ManyToManyThrowsForNotFoundFixture.cs @@ -44,6 +44,7 @@ protected override void OnTearDown() [Test] public void LazyLoad() { + using var log = new SqlLogSpy(); using var s = OpenSession(); var device = s.Get(_id); Assert.Throws(() => NHibernateUtil.Initialize(device.DrivesNotIgnored)); diff --git a/src/NHibernate.Test/NHSpecificTest/NH750/Mappings.hbm.xml b/src/NHibernate.Test/NHSpecificTest/NH750/Mappings.hbm.xml index 56d7c9c5927..f0779fc2d1a 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH750/Mappings.hbm.xml +++ b/src/NHibernate.Test/NHSpecificTest/NH750/Mappings.hbm.xml @@ -2,13 +2,13 @@ + default-access="field.camelcase-underscore"> + diff --git a/src/NHibernate.Test/SubselectFetchTest/SubselectFetchFixture.cs b/src/NHibernate.Test/SubselectFetchTest/SubselectFetchFixture.cs index 9fd00d6e6c6..46f7034514f 100644 --- a/src/NHibernate.Test/SubselectFetchTest/SubselectFetchFixture.cs +++ b/src/NHibernate.Test/SubselectFetchTest/SubselectFetchFixture.cs @@ -280,7 +280,7 @@ public void ManyToManyCriteriaJoin() s = OpenSession(); t = s.BeginTransaction(); - + var log = new SqlLogSpy(); s.CreateCriteria(typeof(Parent)) .AddOrder(Order.Desc("Name")) // H3 has this after CreateCriteria("Friends"), but it's not yet supported in NH