-
Notifications
You must be signed in to change notification settings - Fork 933
Fix caching linq query with ThenFetchMany #2558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hazzik
merged 5 commits into
nhibernate:5.3.x
from
illkostinf:CachingQueryWithThenFetchMany
Sep 23, 2020
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
src/NHibernate.Test/Async/NHSpecificTest/GH2559/FixtureByCode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// This code was generated by AsyncGenerator. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
|
||
|
||
using System.Linq; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Linq; | ||
using NHibernate.Mapping.ByCode; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2559 | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class ByCodeFixtureAsync : TestCaseMappingByCode | ||
{ | ||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Person>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
rc.Property(x => x.Name); | ||
rc.Property(x => x.Age); | ||
rc.Set( | ||
x => x.Children, | ||
colMap => | ||
{ | ||
colMap.Inverse(true); | ||
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); | ||
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}, | ||
rel => rel.OneToMany()); | ||
rc.Set( | ||
x => x.Cars, | ||
colMap => | ||
{ | ||
colMap.Inverse(true); | ||
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); | ||
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}, | ||
rel => rel.OneToMany()); | ||
rc.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
mapper.Class<Child>(ch => | ||
{ | ||
ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
ch.Property(x => x.Name); | ||
ch.ManyToOne(c => c.Parent); | ||
|
||
ch.Set( | ||
x => x.Pets, | ||
colMap => | ||
{ | ||
colMap.Inverse(true); | ||
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); | ||
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}, | ||
rel => rel.OneToMany()); | ||
|
||
ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
mapper.Class<Pet>(ch => | ||
{ | ||
ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
ch.Property(x => x.Name); | ||
ch.ManyToOne(c => c.Owner); | ||
|
||
ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
mapper.Class<Car>(ch => | ||
{ | ||
ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
ch.Property(x => x.Name); | ||
ch.ManyToOne(c => c.Owner); | ||
|
||
ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var person = new Person { Name = "Person 1", Age = 18 }; | ||
|
||
var car1 = new Car { Name = "Car1", Owner = person }; | ||
var car2 = new Car { Name = "Car2", Owner = person }; | ||
session.Save(car1); | ||
session.Save(car2); | ||
|
||
session.Save(person); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from Pet").ExecuteUpdate(); | ||
session.CreateQuery("delete from Child").ExecuteUpdate(); | ||
session.CreateQuery("delete from Car").ExecuteUpdate(); | ||
session.CreateQuery("delete from Person").ExecuteUpdate(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public async Task TestQueryCachingWithThenFetchManyAsync() | ||
{ | ||
Person dbPerson; | ||
Person cachePerson; | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var query = | ||
session | ||
.Query<Person>() | ||
.FetchMany(p => p.Children) | ||
.ThenFetchMany(ch => ch.Pets) | ||
.FetchMany(p => p.Cars) as IQueryable<Person>; | ||
|
||
query = query.WithOptions(opt => | ||
opt.SetCacheable(true) | ||
.SetCacheMode(CacheMode.Normal) | ||
.SetCacheRegion("Long_Cache")); | ||
|
||
dbPerson = (await (query.ToListAsync())).First(); // First time the result will be cached | ||
cachePerson = (await (query.ToListAsync())).First(); | ||
|
||
await (transaction.CommitAsync()); | ||
} | ||
|
||
Assert.That(NHibernateUtil.IsInitialized(dbPerson.Cars), Is.True); | ||
Assert.That(NHibernateUtil.IsInitialized(cachePerson.Cars), Is.True); | ||
Assert.That(dbPerson.Cars, Has.Count.EqualTo(2)); | ||
Assert.That(cachePerson.Cars, Has.Count.EqualTo(2)); | ||
|
||
Assert.That(NHibernateUtil.IsInitialized(dbPerson.Children), Is.True); | ||
Assert.That(NHibernateUtil.IsInitialized(cachePerson.Children), Is.True); | ||
Assert.That(dbPerson.Children, Has.Count.EqualTo(0)); | ||
Assert.That(cachePerson.Children, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2559 | ||
{ | ||
public class Car | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
|
||
public virtual Person Owner { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2559 | ||
{ | ||
public class Child | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
|
||
public virtual Person Parent { get; set; } | ||
|
||
public virtual ISet<Pet> Pets | ||
{ | ||
get => _pets ?? (_pets = new HashSet<Pet>()); | ||
set => _pets = value; | ||
} | ||
private ISet<Pet> _pets; | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
src/NHibernate.Test/NHSpecificTest/GH2559/FixtureByCode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using System.Linq; | ||
using NHibernate.Cfg.MappingSchema; | ||
using NHibernate.Linq; | ||
using NHibernate.Mapping.ByCode; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2559 | ||
{ | ||
[TestFixture] | ||
public class ByCodeFixture : TestCaseMappingByCode | ||
{ | ||
protected override HbmMapping GetMappings() | ||
{ | ||
var mapper = new ModelMapper(); | ||
mapper.Class<Person>(rc => | ||
{ | ||
rc.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
rc.Property(x => x.Name); | ||
rc.Property(x => x.Age); | ||
rc.Set( | ||
x => x.Children, | ||
colMap => | ||
{ | ||
colMap.Inverse(true); | ||
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); | ||
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}, | ||
rel => rel.OneToMany()); | ||
rc.Set( | ||
x => x.Cars, | ||
colMap => | ||
{ | ||
colMap.Inverse(true); | ||
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); | ||
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}, | ||
rel => rel.OneToMany()); | ||
rc.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
mapper.Class<Child>(ch => | ||
{ | ||
ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
ch.Property(x => x.Name); | ||
ch.ManyToOne(c => c.Parent); | ||
|
||
ch.Set( | ||
x => x.Pets, | ||
colMap => | ||
{ | ||
colMap.Inverse(true); | ||
colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); | ||
colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}, | ||
rel => rel.OneToMany()); | ||
|
||
ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
mapper.Class<Pet>(ch => | ||
{ | ||
ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
ch.Property(x => x.Name); | ||
ch.ManyToOne(c => c.Owner); | ||
|
||
ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
mapper.Class<Car>(ch => | ||
{ | ||
ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); | ||
ch.Property(x => x.Name); | ||
ch.ManyToOne(c => c.Owner); | ||
|
||
ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); | ||
}); | ||
|
||
return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
} | ||
|
||
protected override void OnSetUp() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var person = new Person { Name = "Person 1", Age = 18 }; | ||
|
||
var car1 = new Car { Name = "Car1", Owner = person }; | ||
var car2 = new Car { Name = "Car2", Owner = person }; | ||
session.Save(car1); | ||
session.Save(car2); | ||
|
||
session.Save(person); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
session.CreateQuery("delete from Pet").ExecuteUpdate(); | ||
session.CreateQuery("delete from Child").ExecuteUpdate(); | ||
session.CreateQuery("delete from Car").ExecuteUpdate(); | ||
session.CreateQuery("delete from Person").ExecuteUpdate(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void TestQueryCachingWithThenFetchMany() | ||
{ | ||
Person dbPerson; | ||
Person cachePerson; | ||
using (var session = OpenSession()) | ||
using (var transaction = session.BeginTransaction()) | ||
{ | ||
var query = | ||
session | ||
.Query<Person>() | ||
.FetchMany(p => p.Children) | ||
.ThenFetchMany(ch => ch.Pets) | ||
.FetchMany(p => p.Cars) as IQueryable<Person>; | ||
|
||
query = query.WithOptions(opt => | ||
opt.SetCacheable(true) | ||
.SetCacheMode(CacheMode.Normal) | ||
.SetCacheRegion("Long_Cache")); | ||
|
||
dbPerson = query.ToList().First(); // First time the result will be cached | ||
cachePerson = query.ToList().First(); | ||
|
||
transaction.Commit(); | ||
} | ||
|
||
Assert.That(NHibernateUtil.IsInitialized(dbPerson.Cars), Is.True); | ||
Assert.That(NHibernateUtil.IsInitialized(cachePerson.Cars), Is.True); | ||
Assert.That(dbPerson.Cars, Has.Count.EqualTo(2)); | ||
Assert.That(cachePerson.Cars, Has.Count.EqualTo(2)); | ||
|
||
Assert.That(NHibernateUtil.IsInitialized(dbPerson.Children), Is.True); | ||
Assert.That(NHibernateUtil.IsInitialized(cachePerson.Children), Is.True); | ||
Assert.That(dbPerson.Children, Has.Count.EqualTo(0)); | ||
Assert.That(cachePerson.Children, Has.Count.EqualTo(0)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2559 | ||
{ | ||
public class Person | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
public virtual int Age { get; set; } | ||
|
||
public virtual ISet<Car> Cars | ||
{ | ||
get => _cars ?? (_cars = new HashSet<Car>()); | ||
set => _cars = value; | ||
} | ||
private ISet<Car> _cars; | ||
|
||
public virtual ISet<Child> Children | ||
{ | ||
get => _children ?? (_children = new HashSet<Child>()); | ||
set => _children = value; | ||
} | ||
private ISet<Child> _children; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH2559 | ||
{ | ||
public class Pet | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
|
||
public virtual Child Owner { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.