|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using NHibernate.Cfg.MappingSchema; |
| 4 | +using NHibernate.Mapping.ByCode; |
| 5 | +using NHibernate.Linq; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace NHibernate.Test.NHSpecificTest.GH3317 |
| 9 | +{ |
| 10 | + [TestFixture(true)] |
| 11 | + [TestFixture(false)] |
| 12 | + public class ComponentsListFixture : TestCaseMappingByCode |
| 13 | + { |
| 14 | + private readonly bool _fetchJoinMapping; |
| 15 | + private Guid _id; |
| 16 | + |
| 17 | + public ComponentsListFixture(bool fetchJoinMapping) |
| 18 | + { |
| 19 | + _fetchJoinMapping = fetchJoinMapping; |
| 20 | + } |
| 21 | + |
| 22 | + protected override void OnSetUp() |
| 23 | + { |
| 24 | + using var session = OpenSession(); |
| 25 | + using var tr = session.BeginTransaction(); |
| 26 | + var root = new Entity(); |
| 27 | + root.Entries.Add(new ComponentListEntry { ComponentReference = null, DummyString = "one", }); |
| 28 | + |
| 29 | + session.Save(root); |
| 30 | + tr.Commit(); |
| 31 | + _id = root.Id; |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + public void LazyLoading() |
| 36 | + { |
| 37 | + using var newSession = OpenSession(); |
| 38 | + var reloadedRoot = newSession.Get<Entity>(_id); |
| 39 | + Assert.AreEqual(1, reloadedRoot.Entries.Count); |
| 40 | + } |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void QueryOverFetch() |
| 44 | + { |
| 45 | + using var newSession = OpenSession(); |
| 46 | + var reloadedRoot = newSession.QueryOver<Entity>() |
| 47 | + .Fetch(SelectMode.Fetch, x => x.Entries) |
| 48 | + .Where(x => x.Id == _id) |
| 49 | + .SingleOrDefault(); |
| 50 | + Assert.AreEqual(1, reloadedRoot.Entries.Count); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void LinqFetch() |
| 55 | + { |
| 56 | + using var newSession = OpenSession(); |
| 57 | + var reloadedRoot = newSession.Query<Entity>() |
| 58 | + .Fetch(x => x.Entries) |
| 59 | + .Where(x => x.Id == _id) |
| 60 | + .SingleOrDefault(); |
| 61 | + Assert.AreEqual(1, reloadedRoot.Entries.Count); |
| 62 | + } |
| 63 | + |
| 64 | + protected override void OnTearDown() |
| 65 | + { |
| 66 | + using var session = OpenSession(); |
| 67 | + using var transaction = session.BeginTransaction(); |
| 68 | + session.CreateSQLQuery("delete from Entries").ExecuteUpdate(); |
| 69 | + session.Delete("from System.Object"); |
| 70 | + transaction.Commit(); |
| 71 | + } |
| 72 | + |
| 73 | + protected override HbmMapping GetMappings() |
| 74 | + { |
| 75 | + var mapper = new ModelMapper(); |
| 76 | + |
| 77 | + mapper.Class<Entity>(rc => |
| 78 | + { |
| 79 | + rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb)); |
| 80 | + rc.Lazy(false); |
| 81 | + rc.Property(x => x.Name); |
| 82 | + |
| 83 | + rc.Bag( |
| 84 | + x => x.Entries, |
| 85 | + v => |
| 86 | + { |
| 87 | + if (_fetchJoinMapping) |
| 88 | + v.Fetch(CollectionFetchMode.Join); |
| 89 | + }, |
| 90 | + h => h.Component(cmp => |
| 91 | + { |
| 92 | + cmp.Property(x => x.DummyString); |
| 93 | + cmp.ManyToOne(x => x.ComponentReference); |
| 94 | + })); |
| 95 | + }); |
| 96 | + mapper.Class<EntityWithParent>(rc => |
| 97 | + { |
| 98 | + rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb)); |
| 99 | + rc.Lazy(false); |
| 100 | + rc.ManyToOne(x => x.Parent, m => m.NotNullable(true)); |
| 101 | + }); |
| 102 | + mapper.Class<ParentEntity>(rc => |
| 103 | + { |
| 104 | + rc.Id(x => x.Id, map => map.Generator(Generators.GuidComb)); |
| 105 | + rc.Lazy(false); |
| 106 | + rc.Property(x => x.Name); |
| 107 | + }); |
| 108 | + |
| 109 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments