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