|
| 1 | +using System.Linq; |
| 2 | +using NHibernate.Cfg.MappingSchema; |
| 3 | +using NHibernate.Mapping.ByCode; |
| 4 | +using NHibernate.Linq; |
| 5 | +using NUnit.Framework; |
| 6 | + |
| 7 | +namespace NHibernate.Test.NHSpecificTest.CachingComplexQuery |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Fixture using 'by code' mappings |
| 11 | + /// </summary> |
| 12 | + /// <remarks> |
| 13 | + /// This fixture is identical to <see cref="Fixture" /> except the <see cref="Person" /> mapping is performed |
| 14 | + /// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach |
| 15 | + /// if you prefer. |
| 16 | + /// </remarks> |
| 17 | + [TestFixture] |
| 18 | + public class ByCodeFixture : TestCaseMappingByCode |
| 19 | + { |
| 20 | + protected override HbmMapping GetMappings() |
| 21 | + { |
| 22 | + var mapper = new ModelMapper(); |
| 23 | + mapper.Class<Person>(rc => |
| 24 | + { |
| 25 | + rc.Id(x => x.Id, m => m.Generator(Generators.Guid)); |
| 26 | + rc.Property(x => x.Name); |
| 27 | + rc.Property(x => x.Age); |
| 28 | + rc.Set( |
| 29 | + x => x.Children, |
| 30 | + colMap => |
| 31 | + { |
| 32 | + colMap.Inverse(true); |
| 33 | + colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); |
| 34 | + colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); |
| 35 | + }, |
| 36 | + rel => rel.OneToMany()); |
| 37 | + rc.Set( |
| 38 | + x => x.Cars, |
| 39 | + colMap => |
| 40 | + { |
| 41 | + colMap.Inverse(true); |
| 42 | + colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); |
| 43 | + colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); |
| 44 | + }, |
| 45 | + rel => rel.OneToMany()); |
| 46 | + rc.Cache(c => c.Usage(CacheUsage.ReadWrite)); |
| 47 | + }); |
| 48 | + mapper.Class<Child>(ch => |
| 49 | + { |
| 50 | + ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); |
| 51 | + ch.Property(x => x.Name); |
| 52 | + ch.ManyToOne(c => c.Parent); |
| 53 | + |
| 54 | + ch.Set( |
| 55 | + x => x.Pets, |
| 56 | + colMap => |
| 57 | + { |
| 58 | + colMap.Inverse(true); |
| 59 | + colMap.Cascade(Mapping.ByCode.Cascade.DeleteOrphans); |
| 60 | + colMap.Cache(c => c.Usage(CacheUsage.ReadWrite)); |
| 61 | + }, |
| 62 | + rel => rel.OneToMany()); |
| 63 | + |
| 64 | + ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); |
| 65 | + }); |
| 66 | + mapper.Class<Pet>(ch => |
| 67 | + { |
| 68 | + ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); |
| 69 | + ch.Property(x => x.Name); |
| 70 | + ch.ManyToOne(c => c.Owner); |
| 71 | + |
| 72 | + ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); |
| 73 | + }); |
| 74 | + mapper.Class<Car>(ch => |
| 75 | + { |
| 76 | + ch.Id(x => x.Id, m => m.Generator(Generators.Guid)); |
| 77 | + ch.Property(x => x.Name); |
| 78 | + ch.ManyToOne(c => c.Owner); |
| 79 | + |
| 80 | + ch.Cache(c => c.Usage(CacheUsage.ReadWrite)); |
| 81 | + }); |
| 82 | + |
| 83 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 84 | + } |
| 85 | + |
| 86 | + protected override void OnSetUp() |
| 87 | + { |
| 88 | + using (var session = OpenSession()) |
| 89 | + using (var transaction = session.BeginTransaction()) |
| 90 | + { |
| 91 | + var person = new Person { Name = "Person 1", Age = 18 }; |
| 92 | + |
| 93 | + var car1 = new Car { Name = "Car1", Owner = person }; |
| 94 | + var car2 = new Car { Name = "Car2", Owner = person }; |
| 95 | + session.Save(car1); |
| 96 | + session.Save(car2); |
| 97 | + |
| 98 | + session.Save(person); |
| 99 | + transaction.Commit(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + protected override void OnTearDown() |
| 104 | + { |
| 105 | + using (var session = OpenSession()) |
| 106 | + using (var transaction = session.BeginTransaction()) |
| 107 | + { |
| 108 | + session.Delete("from Pet"); |
| 109 | + session.Delete("from Child"); |
| 110 | + session.Delete("from Car"); |
| 111 | + session.Delete("from Person"); |
| 112 | + |
| 113 | + transaction.Commit(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + [Test] |
| 118 | + public void TestQueryCachingWithThenFetchMany() |
| 119 | + { |
| 120 | + using (var session = OpenSession()) |
| 121 | + { |
| 122 | + using (var transaction = session.BeginTransaction()) |
| 123 | + { |
| 124 | + var query = |
| 125 | + session |
| 126 | + .Query<Person>() |
| 127 | + .FetchMany(p => p.Children) |
| 128 | + .ThenFetchMany(ch => ch.Pets) |
| 129 | + .FetchMany(p => p.Cars) as IQueryable<Person>; |
| 130 | + |
| 131 | + query = query.WithOptions(opt => |
| 132 | + opt.SetCacheable(true) |
| 133 | + .SetCacheMode(CacheMode.Normal) |
| 134 | + .SetCacheRegion("Long_Cache")); |
| 135 | + |
| 136 | + query.ToList(); // First time the result will be cached |
| 137 | + Assert.That(() => query.ToList(), Throws.Nothing); // |
| 138 | + transaction.Commit(); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments