|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// <auto-generated> |
| 3 | +// This code was generated by AsyncGenerator. |
| 4 | +// |
| 5 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 6 | +// the code is regenerated. |
| 7 | +// </auto-generated> |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | + |
| 11 | +using System.Collections.Generic; |
| 12 | +using NHibernate.Cfg; |
| 13 | +using NHibernate.Cfg.MappingSchema; |
| 14 | +using NHibernate.Mapping.ByCode; |
| 15 | +using NHibernate.Transform; |
| 16 | +using NUnit.Framework; |
| 17 | + |
| 18 | +namespace NHibernate.Test.NHSpecificTest.GH3290 |
| 19 | +{ |
| 20 | + using System.Threading.Tasks; |
| 21 | + [TestFixture(true)] |
| 22 | + [TestFixture(false)] |
| 23 | + public class FixtureAsync : TestCaseMappingByCode |
| 24 | + { |
| 25 | + private readonly bool _detectFetchLoops; |
| 26 | + |
| 27 | + public FixtureAsync(bool detectFetchLoops) |
| 28 | + { |
| 29 | + _detectFetchLoops = detectFetchLoops; |
| 30 | + } |
| 31 | + |
| 32 | + protected override HbmMapping GetMappings() |
| 33 | + { |
| 34 | + var mapper = new ModelMapper(); |
| 35 | + mapper.Class<Entity>(rc => |
| 36 | + { |
| 37 | + rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb)); |
| 38 | + |
| 39 | + rc.Property( |
| 40 | + x => x.Name |
| 41 | + ); |
| 42 | + |
| 43 | + rc.Set( |
| 44 | + x => x.Children, |
| 45 | + v => |
| 46 | + { |
| 47 | + v.Table("EntityToEntity"); |
| 48 | + v.Cascade(Mapping.ByCode.Cascade.None); |
| 49 | + v.Inverse(true); |
| 50 | + v.Key(x => |
| 51 | + { |
| 52 | + x.Column("ParentId"); |
| 53 | + x.NotNullable(true); |
| 54 | + }); |
| 55 | + v.Lazy(CollectionLazy.Lazy); |
| 56 | + v.Fetch(CollectionFetchMode.Join); |
| 57 | + }, |
| 58 | + h => h.ManyToMany(m => m.Column("ChildId")) |
| 59 | + ); |
| 60 | + |
| 61 | + rc.Set( |
| 62 | + x => x.Parents, |
| 63 | + v => |
| 64 | + { |
| 65 | + v.Table("EntityToEntity"); |
| 66 | + v.Cascade(Mapping.ByCode.Cascade.All); |
| 67 | + |
| 68 | + v.Key(x => |
| 69 | + { |
| 70 | + x.Column("ChildId"); |
| 71 | + x.NotNullable(true); |
| 72 | + }); |
| 73 | + v.Lazy(CollectionLazy.Lazy); |
| 74 | + v.Fetch(CollectionFetchMode.Join); |
| 75 | + }, |
| 76 | + h => h.ManyToMany(m => m.Column("ParentId")) |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 81 | + } |
| 82 | + |
| 83 | + protected override void Configure(Configuration configuration) |
| 84 | + { |
| 85 | + configuration.SetProperty(Environment.DetectFetchLoops, _detectFetchLoops ? "true" : "false"); |
| 86 | + configuration.SetProperty(Environment.MaxFetchDepth, _detectFetchLoops ? "-1" : "2"); |
| 87 | + } |
| 88 | + |
| 89 | + protected override void OnSetUp() |
| 90 | + { |
| 91 | + using var session = OpenSession(); |
| 92 | + using var transaction = session.BeginTransaction(); |
| 93 | + |
| 94 | + var person = new Entity |
| 95 | + { |
| 96 | + Name = "pers", |
| 97 | + Parents = new HashSet<Entity>() |
| 98 | + }; |
| 99 | + session.Save(person); |
| 100 | + var job = new Entity |
| 101 | + { |
| 102 | + Name = "job", |
| 103 | + Children = new HashSet<Entity>() |
| 104 | + }; |
| 105 | + session.Save(job); |
| 106 | + |
| 107 | + job.Children.Add(person); |
| 108 | + person.Parents.Add(job); |
| 109 | + |
| 110 | + transaction.Commit(); |
| 111 | + } |
| 112 | + |
| 113 | + protected override void OnTearDown() |
| 114 | + { |
| 115 | + using var session = OpenSession(); |
| 116 | + using var transaction = session.BeginTransaction(); |
| 117 | + |
| 118 | + session.CreateSQLQuery("delete from EntityToEntity").ExecuteUpdate(); |
| 119 | + session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
| 120 | + |
| 121 | + transaction.Commit(); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public async Task QueryWithFetchAsync() |
| 126 | + { |
| 127 | + using var session = OpenSession(); |
| 128 | + using var _ = session.BeginTransaction(); |
| 129 | + |
| 130 | + var all = await (session |
| 131 | + .QueryOver<Entity>() |
| 132 | + .Fetch(SelectMode.Fetch, x => x.Children) |
| 133 | + .Fetch(SelectMode.Fetch, x => x.Parents) |
| 134 | + .TransformUsing(Transformers.DistinctRootEntity) |
| 135 | + .ListAsync()); |
| 136 | + |
| 137 | + foreach (var entity in all) |
| 138 | + { |
| 139 | + var isPerson = entity.Name == "pers"; |
| 140 | + if (isPerson) |
| 141 | + Assert.That(entity.Parents, Has.Count.EqualTo(1), "Person's job not found or non-unique."); |
| 142 | + else |
| 143 | + Assert.That(entity.Children, Has.Count.EqualTo(1), "Job's employee not found or non-unique."); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments