-
Notifications
You must be signed in to change notification settings - Fork 932
Fix many-to-many fetch issue #3316
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
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efdcbf5
Add test case for #3290
fredericDelaporte 371fc87
Enable both cases
bahusoid b845845
Generate async files
github-actions[bot] b33095e
Fix
bahusoid 897f837
Fix SqlServerCe for #3314
bahusoid c40a391
Explicit fetch loop detection configuration
bahusoid 71b3d7c
Merge branch '5.4.x' into GH3290
bahusoid 433ff19
Generate async files
github-actions[bot] 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
150 changes: 150 additions & 0 deletions
150
src/NHibernate.Test/Async/NHSpecificTest/GH3290/Fixture.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,150 @@ | ||
| //------------------------------------------------------------------------------ | ||
| // <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.Collections.Generic; | ||
| using NHibernate.Cfg; | ||
| using NHibernate.Cfg.MappingSchema; | ||
| using NHibernate.Mapping.ByCode; | ||
| using NHibernate.Transform; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace NHibernate.Test.NHSpecificTest.GH3290 | ||
| { | ||
| using System.Threading.Tasks; | ||
| [TestFixture(true)] | ||
| [TestFixture(false)] | ||
| public class FixtureAsync : TestCaseMappingByCode | ||
| { | ||
| private readonly bool _detectFetchLoops; | ||
|
|
||
| public FixtureAsync(bool detectFetchLoops) | ||
| { | ||
| _detectFetchLoops = detectFetchLoops; | ||
| } | ||
|
|
||
| protected override HbmMapping GetMappings() | ||
| { | ||
| var mapper = new ModelMapper(); | ||
| mapper.Class<Entity>(rc => | ||
| { | ||
| rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb)); | ||
|
|
||
| rc.Property( | ||
| x => x.Name | ||
| ); | ||
|
|
||
| rc.Set( | ||
| x => x.Children, | ||
| v => | ||
| { | ||
| v.Table("EntityToEntity"); | ||
| v.Cascade(Mapping.ByCode.Cascade.None); | ||
| v.Inverse(true); | ||
| v.Key(x => | ||
| { | ||
| x.Column("ParentId"); | ||
| x.NotNullable(true); | ||
| }); | ||
| v.Lazy(CollectionLazy.Lazy); | ||
| v.Fetch(CollectionFetchMode.Join); | ||
| }, | ||
| h => h.ManyToMany(m => m.Column("ChildId")) | ||
| ); | ||
|
|
||
| rc.Set( | ||
| x => x.Parents, | ||
| v => | ||
| { | ||
| v.Table("EntityToEntity"); | ||
| v.Cascade(Mapping.ByCode.Cascade.All); | ||
|
|
||
| v.Key(x => | ||
| { | ||
| x.Column("ChildId"); | ||
| x.NotNullable(true); | ||
| }); | ||
| v.Lazy(CollectionLazy.Lazy); | ||
| v.Fetch(CollectionFetchMode.Join); | ||
| }, | ||
| h => h.ManyToMany(m => m.Column("ParentId")) | ||
| ); | ||
| }); | ||
|
|
||
| return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
| } | ||
|
|
||
| protected override void Configure(Configuration configuration) | ||
| { | ||
| if (_detectFetchLoops) | ||
| return; | ||
|
|
||
| configuration.SetProperty(Environment.DetectFetchLoops, "false"); | ||
| configuration.SetProperty(Environment.MaxFetchDepth, "2"); | ||
| } | ||
|
|
||
| protected override void OnSetUp() | ||
| { | ||
| using var session = OpenSession(); | ||
| using var transaction = session.BeginTransaction(); | ||
|
|
||
| var person = new Entity | ||
| { | ||
| Name = "pers", | ||
| Parents = new HashSet<Entity>() | ||
| }; | ||
| session.Save(person); | ||
| var job = new Entity | ||
| { | ||
| Name = "job", | ||
| Children = new HashSet<Entity>() | ||
| }; | ||
| session.Save(job); | ||
|
|
||
| job.Children.Add(person); | ||
| person.Parents.Add(job); | ||
|
|
||
| transaction.Commit(); | ||
| } | ||
|
|
||
| protected override void OnTearDown() | ||
| { | ||
| using var session = OpenSession(); | ||
| using var transaction = session.BeginTransaction(); | ||
|
|
||
| session.CreateSQLQuery("delete from EntityToEntity").ExecuteUpdate(); | ||
| session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
|
||
| transaction.Commit(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task QueryWithFetchAsync() | ||
| { | ||
| using var session = OpenSession(); | ||
| using var _ = session.BeginTransaction(); | ||
|
|
||
| var all = await (session | ||
| .QueryOver<Entity>() | ||
| .Fetch(SelectMode.Fetch, x => x.Children) | ||
| .Fetch(SelectMode.Fetch, x => x.Parents) | ||
| .TransformUsing(Transformers.DistinctRootEntity) | ||
| .ListAsync()); | ||
|
|
||
| foreach (var entity in all) | ||
| { | ||
| var isPerson = entity.Name == "pers"; | ||
| if (isPerson) | ||
| Assert.That(entity.Parents, Has.Count.EqualTo(1), "Person's job not found or non-unique."); | ||
| else | ||
| Assert.That(entity.Children, Has.Count.EqualTo(1), "Job's employee not found or non-unique."); | ||
| } | ||
| } | ||
| } | ||
| } |
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,13 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace NHibernate.Test.NHSpecificTest.GH3290 | ||
| { | ||
| class Entity | ||
| { | ||
| public virtual Guid Id { get; set; } | ||
| public virtual string Name { get; set; } | ||
| public virtual ISet<Entity> Parents { get; set; } | ||
| public virtual ISet<Entity> Children { 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,139 @@ | ||
| using System.Collections.Generic; | ||
| using NHibernate.Cfg; | ||
| using NHibernate.Cfg.MappingSchema; | ||
| using NHibernate.Mapping.ByCode; | ||
| using NHibernate.Transform; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace NHibernate.Test.NHSpecificTest.GH3290 | ||
| { | ||
| [TestFixture(true)] | ||
| [TestFixture(false)] | ||
| public class Fixture : TestCaseMappingByCode | ||
| { | ||
| private readonly bool _detectFetchLoops; | ||
|
|
||
| public Fixture(bool detectFetchLoops) | ||
| { | ||
| _detectFetchLoops = detectFetchLoops; | ||
| } | ||
|
|
||
| protected override HbmMapping GetMappings() | ||
| { | ||
| var mapper = new ModelMapper(); | ||
| mapper.Class<Entity>(rc => | ||
| { | ||
| rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb)); | ||
|
|
||
| rc.Property( | ||
| x => x.Name | ||
| ); | ||
|
|
||
| rc.Set( | ||
| x => x.Children, | ||
| v => | ||
| { | ||
| v.Table("EntityToEntity"); | ||
| v.Cascade(Mapping.ByCode.Cascade.None); | ||
| v.Inverse(true); | ||
| v.Key(x => | ||
| { | ||
| x.Column("ParentId"); | ||
| x.NotNullable(true); | ||
| }); | ||
| v.Lazy(CollectionLazy.Lazy); | ||
| v.Fetch(CollectionFetchMode.Join); | ||
| }, | ||
| h => h.ManyToMany(m => m.Column("ChildId")) | ||
| ); | ||
|
|
||
| rc.Set( | ||
| x => x.Parents, | ||
| v => | ||
| { | ||
| v.Table("EntityToEntity"); | ||
| v.Cascade(Mapping.ByCode.Cascade.All); | ||
|
|
||
| v.Key(x => | ||
| { | ||
| x.Column("ChildId"); | ||
| x.NotNullable(true); | ||
| }); | ||
| v.Lazy(CollectionLazy.Lazy); | ||
| v.Fetch(CollectionFetchMode.Join); | ||
| }, | ||
| h => h.ManyToMany(m => m.Column("ParentId")) | ||
| ); | ||
| }); | ||
|
|
||
| return mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||
| } | ||
|
|
||
| protected override void Configure(Configuration configuration) | ||
| { | ||
| if (_detectFetchLoops) | ||
| return; | ||
|
|
||
| configuration.SetProperty(Environment.DetectFetchLoops, "false"); | ||
| configuration.SetProperty(Environment.MaxFetchDepth, "2"); | ||
| } | ||
|
|
||
| protected override void OnSetUp() | ||
| { | ||
| using var session = OpenSession(); | ||
| using var transaction = session.BeginTransaction(); | ||
|
|
||
| var person = new Entity | ||
| { | ||
| Name = "pers", | ||
| Parents = new HashSet<Entity>() | ||
| }; | ||
| session.Save(person); | ||
| var job = new Entity | ||
| { | ||
| Name = "job", | ||
| Children = new HashSet<Entity>() | ||
| }; | ||
| session.Save(job); | ||
|
|
||
| job.Children.Add(person); | ||
| person.Parents.Add(job); | ||
|
|
||
| transaction.Commit(); | ||
| } | ||
|
|
||
| protected override void OnTearDown() | ||
| { | ||
| using var session = OpenSession(); | ||
| using var transaction = session.BeginTransaction(); | ||
|
|
||
| session.CreateSQLQuery("delete from EntityToEntity").ExecuteUpdate(); | ||
| session.CreateQuery("delete from System.Object").ExecuteUpdate(); | ||
|
|
||
| transaction.Commit(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void QueryWithFetch() | ||
| { | ||
| using var session = OpenSession(); | ||
| using var _ = session.BeginTransaction(); | ||
|
|
||
| var all = session | ||
| .QueryOver<Entity>() | ||
| .Fetch(SelectMode.Fetch, x => x.Children) | ||
| .Fetch(SelectMode.Fetch, x => x.Parents) | ||
| .TransformUsing(Transformers.DistinctRootEntity) | ||
| .List(); | ||
|
|
||
| foreach (var entity in all) | ||
| { | ||
| var isPerson = entity.Name == "pers"; | ||
| if (isPerson) | ||
| Assert.That(entity.Parents, Has.Count.EqualTo(1), "Person's job not found or non-unique."); | ||
| else | ||
| Assert.That(entity.Children, Has.Count.EqualTo(1), "Job's employee not found or non-unique."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.