Skip to content

Commit 1148aa9

Browse files
committed
Unexpected query for Future with non-lazy association
1 parent bc3f56b commit 1148aa9

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

src/NHibernate.Test/Async/Futures/QueryBatchFixture.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,33 @@ public async Task CacheModeWorksWithFutureAsync()
498498
}
499499
}
500500

501+
//GH-2173
502+
[Test]
503+
public async Task CanFetchNonLazyEntitiesInSubsequentQueryAsync()
504+
{
505+
Sfi.Statistics.IsStatisticsEnabled = true;
506+
using (var s = OpenSession())
507+
using (var t = s.BeginTransaction())
508+
{
509+
await (s.SaveAsync(
510+
new EntityEager
511+
{
512+
Name = "EagerManyToOneAssociation",
513+
EagerEntity = new EntityEagerChild {Name = "association"}
514+
}));
515+
await (t.CommitAsync());
516+
}
517+
518+
using (var s = OpenSession())
519+
{
520+
Sfi.Statistics.Clear();
521+
//EntityEager.EagerEntity is lazy initialized instead of being loaded by the second query
522+
s.QueryOver<EntityEager>().Fetch(SelectMode.Skip, x => x.EagerEntity).Future();
523+
s.QueryOver<EntityEager>().Fetch(SelectMode.Fetch, x => x.EagerEntity).Future().ToList();
524+
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
525+
}
526+
}
527+
501528
#region Test Setup
502529

503530
protected override HbmMapping GetMappings()
@@ -543,6 +570,10 @@ protected override HbmMapping GetMappings()
543570
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
544571
rc.Property(x => x.Name);
545572

573+
rc.ManyToOne(x => x.EagerEntity, m =>
574+
{
575+
m.Cascade(Mapping.ByCode.Cascade.Persist);
576+
});
546577
rc.Bag(ep => ep.ChildrenListSubselect,
547578
m =>
548579
{
@@ -560,6 +591,14 @@ protected override HbmMapping GetMappings()
560591
},
561592
a => a.OneToMany());
562593
});
594+
mapper.Class<EntityEagerChild>(
595+
rc =>
596+
{
597+
rc.Lazy(false);
598+
599+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
600+
rc.Property(x => x.Name);
601+
});
563602
mapper.Class<EntitySubselectChild>(
564603
rc =>
565604
{

src/NHibernate.Test/Futures/Entities.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ public class EntitySubselectChild
3636
public virtual EntityEager Parent { get; set; }
3737
}
3838

39+
public class EntityEagerChild
40+
{
41+
public Guid Id { get; set; }
42+
public string Name { get; set; }
43+
}
44+
3945
public class EntityEager
4046
{
4147
public Guid Id { get; set; }
4248
public string Name { get; set; }
4349

50+
public EntityEagerChild EagerEntity { get; set; }
4451
public IList<EntitySubselectChild> ChildrenListSubselect { get; set; }
4552
public IList<EntitySimpleChild> ChildrenListEager { get; set; } //= new HashSet<EntitySimpleChild>();
4653
}

src/NHibernate.Test/Futures/QueryBatchFixture.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,33 @@ public void CacheModeWorksWithFuture()
486486
}
487487
}
488488

489+
//GH-2173
490+
[Test]
491+
public void CanFetchNonLazyEntitiesInSubsequentQuery()
492+
{
493+
Sfi.Statistics.IsStatisticsEnabled = true;
494+
using (var s = OpenSession())
495+
using (var t = s.BeginTransaction())
496+
{
497+
s.Save(
498+
new EntityEager
499+
{
500+
Name = "EagerManyToOneAssociation",
501+
EagerEntity = new EntityEagerChild {Name = "association"}
502+
});
503+
t.Commit();
504+
}
505+
506+
using (var s = OpenSession())
507+
{
508+
Sfi.Statistics.Clear();
509+
//EntityEager.EagerEntity is lazy initialized instead of being loaded by the second query
510+
s.QueryOver<EntityEager>().Fetch(SelectMode.Skip, x => x.EagerEntity).Future();
511+
s.QueryOver<EntityEager>().Fetch(SelectMode.Fetch, x => x.EagerEntity).Future().ToList();
512+
Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1));
513+
}
514+
}
515+
489516
#region Test Setup
490517

491518
protected override HbmMapping GetMappings()
@@ -531,6 +558,10 @@ protected override HbmMapping GetMappings()
531558
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
532559
rc.Property(x => x.Name);
533560

561+
rc.ManyToOne(x => x.EagerEntity, m =>
562+
{
563+
m.Cascade(Mapping.ByCode.Cascade.Persist);
564+
});
534565
rc.Bag(ep => ep.ChildrenListSubselect,
535566
m =>
536567
{
@@ -548,6 +579,14 @@ protected override HbmMapping GetMappings()
548579
},
549580
a => a.OneToMany());
550581
});
582+
mapper.Class<EntityEagerChild>(
583+
rc =>
584+
{
585+
rc.Lazy(false);
586+
587+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
588+
rc.Property(x => x.Name);
589+
});
551590
mapper.Class<EntitySubselectChild>(
552591
rc =>
553592
{

0 commit comments

Comments
 (0)