|
| 1 | +using System.Linq; |
| 2 | +using NHibernate.Cfg.MappingSchema; |
| 3 | +using NHibernate.Mapping.ByCode; |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3135 |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class MappingByCodeTest |
| 10 | + { |
| 11 | + [Test] |
| 12 | + public void Bag_InBaseEntity_WithDifferentTables_ShouldBeMappedAccordingly() |
| 13 | + { |
| 14 | + var mapper = new ModelMapper(); |
| 15 | + mapper.Class<Entity1>(cm => |
| 16 | + { |
| 17 | + cm.Table("Legacy1"); |
| 18 | + cm.Id(p => p.Id); |
| 19 | + cm.Property(p => p.Foo); |
| 20 | + cm.Bag(p => p.ComponentCollection, |
| 21 | + m => |
| 22 | + { |
| 23 | + m.Access(Accessor.Field); |
| 24 | + m.Fetch(CollectionFetchMode.Select); |
| 25 | + m.Table("Legacy1_Comp"); |
| 26 | + }, |
| 27 | + m => m.Component(c => |
| 28 | + { |
| 29 | + c.Property(p => p.Property1); |
| 30 | + c.Property(p => p.Property2); |
| 31 | + })); |
| 32 | + }); |
| 33 | + mapper.Class<Entity2>(cm => |
| 34 | + { |
| 35 | + cm.Table("Legacy2"); |
| 36 | + cm.Id(p => p.Id); |
| 37 | + cm.Property(p => p.Bar); |
| 38 | + cm.Bag(p => p.ComponentCollection, |
| 39 | + m => |
| 40 | + { |
| 41 | + m.Access(Accessor.Field); |
| 42 | + m.Fetch(CollectionFetchMode.Select); |
| 43 | + m.Table("Legacy2_Comp"); |
| 44 | + }, |
| 45 | + m => m.Component(c => |
| 46 | + { |
| 47 | + c.Property(p => p.Property1); |
| 48 | + c.Property(p => p.Property2); |
| 49 | + })); |
| 50 | + }); |
| 51 | + mapper.Class<Entity3>(cm => |
| 52 | + { |
| 53 | + cm.Id(p => p.Id); |
| 54 | + cm.Bag(p => p.ComponentCollection, |
| 55 | + m => |
| 56 | + { |
| 57 | + m.Access(Accessor.Field); |
| 58 | + m.Fetch(CollectionFetchMode.Select); |
| 59 | + }, |
| 60 | + m => m.Component(c => |
| 61 | + { |
| 62 | + c.Property(p => p.Property1); |
| 63 | + c.Property(p => p.Property2); |
| 64 | + })); |
| 65 | + cm.Property(p => p.Baz); |
| 66 | + }); |
| 67 | + //set some defaults: |
| 68 | + //our bags are by default stored in a table called RootType_Component (unless overridden by the mapping) |
| 69 | + mapper.BeforeMapBag += (mi, m, pc) => pc.Table(m.GetRootMember().ReflectedType.Name + "_Component"); |
| 70 | + //all entities are by default stored in a table called Type |
| 71 | + mapper.BeforeMapClass += (mi, t, pc) => pc.Table(t.Name); |
| 72 | + var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 73 | + |
| 74 | + var classMappings = mappings.Items.OfType<HbmClass>(); |
| 75 | + Assert.That(classMappings.Count(), Is.EqualTo(3)); |
| 76 | + var e1Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity1).Name); |
| 77 | + Assert.That(e1Mapping, Is.Not.Null); |
| 78 | + Assert.That(e1Mapping.table, Is.EqualTo("Legacy1")); |
| 79 | + |
| 80 | + var e1Bag = e1Mapping.Items.OfType<HbmBag>().FirstOrDefault(); |
| 81 | + Assert.That(e1Bag, Is.Not.Null); |
| 82 | + Assert.That(e1Bag.Table, Is.EqualTo("Legacy1_Comp")); |
| 83 | + |
| 84 | + var e2Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity2).Name); |
| 85 | + Assert.That(e2Mapping, Is.Not.Null); |
| 86 | + Assert.That(e2Mapping.table, Is.EqualTo("Legacy2")); |
| 87 | + |
| 88 | + var e2Bag = e2Mapping.Items.OfType<HbmBag>().FirstOrDefault(); |
| 89 | + Assert.That(e2Bag, Is.Not.Null); |
| 90 | + Assert.That(e2Bag.Table, Is.EqualTo("Legacy2_Comp")); |
| 91 | + |
| 92 | + var e3Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity3).Name); |
| 93 | + Assert.That(e3Mapping, Is.Not.Null); |
| 94 | + Assert.That(e3Mapping.table, Is.EqualTo("Entity3")); |
| 95 | + |
| 96 | + var e3Bag = e3Mapping.Items.OfType<HbmBag>().FirstOrDefault(); |
| 97 | + Assert.That(e3Bag, Is.Not.Null); |
| 98 | + Assert.That(e3Bag.Table, Is.EqualTo("Entity3_Component")); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void Set_InBaseEntity_WithDifferentTables_ShouldBeMappedAccordingly() |
| 103 | + { |
| 104 | + var mapper = new ModelMapper(); |
| 105 | + mapper.Class<Entity1>(cm => |
| 106 | + { |
| 107 | + cm.Table("Legacy1"); |
| 108 | + cm.Id(p => p.Id); |
| 109 | + cm.Property(p => p.Foo); |
| 110 | + cm.Set(p => p.ComponentCollection, |
| 111 | + m => |
| 112 | + { |
| 113 | + m.Access(Accessor.Field); |
| 114 | + m.Fetch(CollectionFetchMode.Select); |
| 115 | + m.Table("Legacy1_Comp"); |
| 116 | + }, |
| 117 | + m => m.Component(c => |
| 118 | + { |
| 119 | + c.Property(p => p.Property1); |
| 120 | + c.Property(p => p.Property2); |
| 121 | + })); |
| 122 | + }); |
| 123 | + mapper.Class<Entity2>(cm => |
| 124 | + { |
| 125 | + cm.Table("Legacy2"); |
| 126 | + cm.Id(p => p.Id); |
| 127 | + cm.Property(p => p.Bar); |
| 128 | + cm.Set(p => p.ComponentCollection, |
| 129 | + m => |
| 130 | + { |
| 131 | + m.Access(Accessor.Field); |
| 132 | + m.Fetch(CollectionFetchMode.Select); |
| 133 | + m.Table("Legacy2_Comp"); |
| 134 | + }, |
| 135 | + m => m.Component(c => |
| 136 | + { |
| 137 | + c.Property(p => p.Property1); |
| 138 | + c.Property(p => p.Property2); |
| 139 | + })); |
| 140 | + }); |
| 141 | + mapper.Class<Entity3>(cm => |
| 142 | + { |
| 143 | + cm.Id(p => p.Id); |
| 144 | + cm.Set(p => p.ComponentCollection, |
| 145 | + m => |
| 146 | + { |
| 147 | + m.Access(Accessor.Field); |
| 148 | + m.Fetch(CollectionFetchMode.Select); |
| 149 | + }, |
| 150 | + m => m.Component(c => |
| 151 | + { |
| 152 | + c.Property(p => p.Property1); |
| 153 | + c.Property(p => p.Property2); |
| 154 | + })); |
| 155 | + cm.Property(p => p.Baz); |
| 156 | + }); |
| 157 | + //set some defaults: |
| 158 | + //our bags are by default stored in a table called RootType_Component (unless overridden by the mapping) |
| 159 | + mapper.BeforeMapSet += (mi, m, pc) => pc.Table(m.GetRootMember().ReflectedType.Name + "_Component"); |
| 160 | + //all entities are by default stored in a table called Type |
| 161 | + mapper.BeforeMapClass += (mi, t, pc) => pc.Table(t.Name); |
| 162 | + var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 163 | + |
| 164 | + var classMappings = mappings.Items.OfType<HbmClass>(); |
| 165 | + Assert.That(classMappings.Count(), Is.EqualTo(3)); |
| 166 | + var e1Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity1).Name); |
| 167 | + Assert.That(e1Mapping, Is.Not.Null); |
| 168 | + Assert.That(e1Mapping.table, Is.EqualTo("Legacy1")); |
| 169 | + |
| 170 | + var e1Bag = e1Mapping.Items.OfType<HbmSet>().FirstOrDefault(); |
| 171 | + Assert.That(e1Bag, Is.Not.Null); |
| 172 | + Assert.That(e1Bag.Table, Is.EqualTo("Legacy1_Comp")); |
| 173 | + |
| 174 | + var e2Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity2).Name); |
| 175 | + Assert.That(e2Mapping, Is.Not.Null); |
| 176 | + Assert.That(e2Mapping.table, Is.EqualTo("Legacy2")); |
| 177 | + |
| 178 | + var e2Bag = e2Mapping.Items.OfType<HbmSet>().FirstOrDefault(); |
| 179 | + Assert.That(e2Bag, Is.Not.Null); |
| 180 | + Assert.That(e2Bag.Table, Is.EqualTo("Legacy2_Comp")); |
| 181 | + |
| 182 | + var e3Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity3).Name); |
| 183 | + Assert.That(e3Mapping, Is.Not.Null); |
| 184 | + Assert.That(e3Mapping.table, Is.EqualTo("Entity3")); |
| 185 | + |
| 186 | + var e3Bag = e3Mapping.Items.OfType<HbmSet>().FirstOrDefault(); |
| 187 | + Assert.That(e3Bag, Is.Not.Null); |
| 188 | + Assert.That(e3Bag.Table, Is.EqualTo("Entity3_Component")); |
| 189 | + } |
| 190 | + |
| 191 | + [Test] |
| 192 | + public void List_InBaseEntity_WithDifferentTables_ShouldBeMappedAccordingly() |
| 193 | + { |
| 194 | + var mapper = new ModelMapper(); |
| 195 | + mapper.Class<Entity1>(cm => |
| 196 | + { |
| 197 | + cm.Table("Legacy1"); |
| 198 | + cm.Id(p => p.Id); |
| 199 | + cm.Property(p => p.Foo); |
| 200 | + cm.List(p => p.ComponentCollection, |
| 201 | + m => |
| 202 | + { |
| 203 | + m.Access(Accessor.Field); |
| 204 | + m.Fetch(CollectionFetchMode.Select); |
| 205 | + m.Table("Legacy1_Comp"); |
| 206 | + }, |
| 207 | + m => m.Component(c => |
| 208 | + { |
| 209 | + c.Property(p => p.Property1); |
| 210 | + c.Property(p => p.Property2); |
| 211 | + })); |
| 212 | + }); |
| 213 | + mapper.Class<Entity2>(cm => |
| 214 | + { |
| 215 | + cm.Table("Legacy2"); |
| 216 | + cm.Id(p => p.Id); |
| 217 | + cm.Property(p => p.Bar); |
| 218 | + cm.List(p => p.ComponentCollection, |
| 219 | + m => |
| 220 | + { |
| 221 | + m.Access(Accessor.Field); |
| 222 | + m.Fetch(CollectionFetchMode.Select); |
| 223 | + m.Table("Legacy2_Comp"); |
| 224 | + }, |
| 225 | + m => m.Component(c => |
| 226 | + { |
| 227 | + c.Property(p => p.Property1); |
| 228 | + c.Property(p => p.Property2); |
| 229 | + })); |
| 230 | + }); |
| 231 | + mapper.Class<Entity3>(cm => |
| 232 | + { |
| 233 | + cm.Id(p => p.Id); |
| 234 | + cm.List(p => p.ComponentCollection, |
| 235 | + m => |
| 236 | + { |
| 237 | + m.Access(Accessor.Field); |
| 238 | + m.Fetch(CollectionFetchMode.Select); |
| 239 | + }, |
| 240 | + m => m.Component(c => |
| 241 | + { |
| 242 | + c.Property(p => p.Property1); |
| 243 | + c.Property(p => p.Property2); |
| 244 | + })); |
| 245 | + cm.Property(p => p.Baz); |
| 246 | + }); |
| 247 | + //set some defaults: |
| 248 | + //our bags are by default stored in a table called RootType_Component (unless overridden by the mapping) |
| 249 | + mapper.BeforeMapList += (mi, m, pc) => pc.Table(m.GetRootMember().ReflectedType.Name + "_Component"); |
| 250 | + //all entities are by default stored in a table called Type |
| 251 | + mapper.BeforeMapClass += (mi, t, pc) => pc.Table(t.Name); |
| 252 | + var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 253 | + |
| 254 | + var classMappings = mappings.Items.OfType<HbmClass>(); |
| 255 | + Assert.That(classMappings.Count(), Is.EqualTo(3)); |
| 256 | + var e1Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity1).Name); |
| 257 | + Assert.That(e1Mapping, Is.Not.Null); |
| 258 | + Assert.That(e1Mapping.table, Is.EqualTo("Legacy1")); |
| 259 | + |
| 260 | + var e1Bag = e1Mapping.Items.OfType<HbmList>().FirstOrDefault(); |
| 261 | + Assert.That(e1Bag, Is.Not.Null); |
| 262 | + Assert.That(e1Bag.Table, Is.EqualTo("Legacy1_Comp")); |
| 263 | + |
| 264 | + var e2Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity2).Name); |
| 265 | + Assert.That(e2Mapping, Is.Not.Null); |
| 266 | + Assert.That(e2Mapping.table, Is.EqualTo("Legacy2")); |
| 267 | + |
| 268 | + var e2Bag = e2Mapping.Items.OfType<HbmList>().FirstOrDefault(); |
| 269 | + Assert.That(e2Bag, Is.Not.Null); |
| 270 | + Assert.That(e2Bag.Table, Is.EqualTo("Legacy2_Comp")); |
| 271 | + |
| 272 | + var e3Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity3).Name); |
| 273 | + Assert.That(e3Mapping, Is.Not.Null); |
| 274 | + Assert.That(e3Mapping.table, Is.EqualTo("Entity3")); |
| 275 | + |
| 276 | + var e3Bag = e3Mapping.Items.OfType<HbmList>().FirstOrDefault(); |
| 277 | + Assert.That(e3Bag, Is.Not.Null); |
| 278 | + Assert.That(e3Bag.Table, Is.EqualTo("Entity3_Component")); |
| 279 | + } |
| 280 | + |
| 281 | + [Test] |
| 282 | + public void IdBag_InBaseEntity_WithDifferentTables_ShouldBeMappedAccordingly() |
| 283 | + { |
| 284 | + var mapper = new ModelMapper(); |
| 285 | + mapper.Class<Entity1>(cm => |
| 286 | + { |
| 287 | + cm.Table("Legacy1"); |
| 288 | + cm.Id(p => p.Id); |
| 289 | + cm.Property(p => p.Foo); |
| 290 | + cm.IdBag(p => p.ComponentCollection, |
| 291 | + m => |
| 292 | + { |
| 293 | + m.Access(Accessor.Field); |
| 294 | + m.Fetch(CollectionFetchMode.Select); |
| 295 | + m.Table("Legacy1_Comp"); |
| 296 | + }, |
| 297 | + m => m.Component(c => |
| 298 | + { |
| 299 | + c.Property(p => p.Property1); |
| 300 | + c.Property(p => p.Property2); |
| 301 | + })); |
| 302 | + }); |
| 303 | + mapper.Class<Entity2>(cm => |
| 304 | + { |
| 305 | + cm.Table("Legacy2"); |
| 306 | + cm.Id(p => p.Id); |
| 307 | + cm.Property(p => p.Bar); |
| 308 | + cm.IdBag(p => p.ComponentCollection, |
| 309 | + m => |
| 310 | + { |
| 311 | + m.Access(Accessor.Field); |
| 312 | + m.Fetch(CollectionFetchMode.Select); |
| 313 | + m.Table("Legacy2_Comp"); |
| 314 | + }, |
| 315 | + m => m.Component(c => |
| 316 | + { |
| 317 | + c.Property(p => p.Property1); |
| 318 | + c.Property(p => p.Property2); |
| 319 | + })); |
| 320 | + }); |
| 321 | + mapper.Class<Entity3>(cm => |
| 322 | + { |
| 323 | + cm.Id(p => p.Id); |
| 324 | + cm.IdBag(p => p.ComponentCollection, |
| 325 | + m => |
| 326 | + { |
| 327 | + m.Access(Accessor.Field); |
| 328 | + m.Fetch(CollectionFetchMode.Select); |
| 329 | + }, |
| 330 | + m => m.Component(c => |
| 331 | + { |
| 332 | + c.Property(p => p.Property1); |
| 333 | + c.Property(p => p.Property2); |
| 334 | + })); |
| 335 | + cm.Property(p => p.Baz); |
| 336 | + }); |
| 337 | + //set some defaults: |
| 338 | + //our bags are by default stored in a table called RootType_Component (unless overridden by the mapping) |
| 339 | + mapper.BeforeMapIdBag += (mi, m, pc) => pc.Table(m.GetRootMember().ReflectedType.Name + "_Component"); |
| 340 | + //all entities are by default stored in a table called Type |
| 341 | + mapper.BeforeMapClass += (mi, t, pc) => pc.Table(t.Name); |
| 342 | + var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 343 | + |
| 344 | + var classMappings = mappings.Items.OfType<HbmClass>(); |
| 345 | + Assert.That(classMappings.Count(), Is.EqualTo(3)); |
| 346 | + var e1Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity1).Name); |
| 347 | + Assert.That(e1Mapping, Is.Not.Null); |
| 348 | + Assert.That(e1Mapping.table, Is.EqualTo("Legacy1")); |
| 349 | + |
| 350 | + var e1Bag = e1Mapping.Items.OfType<HbmIdbag>().FirstOrDefault(); |
| 351 | + Assert.That(e1Bag, Is.Not.Null); |
| 352 | + Assert.That(e1Bag.Table, Is.EqualTo("Legacy1_Comp")); |
| 353 | + |
| 354 | + var e2Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity2).Name); |
| 355 | + Assert.That(e2Mapping, Is.Not.Null); |
| 356 | + Assert.That(e2Mapping.table, Is.EqualTo("Legacy2")); |
| 357 | + |
| 358 | + var e2Bag = e2Mapping.Items.OfType<HbmIdbag>().FirstOrDefault(); |
| 359 | + Assert.That(e2Bag, Is.Not.Null); |
| 360 | + Assert.That(e2Bag.Table, Is.EqualTo("Legacy2_Comp")); |
| 361 | + |
| 362 | + var e3Mapping = classMappings.FirstOrDefault(c => c.Name == typeof(Entity3).Name); |
| 363 | + Assert.That(e3Mapping, Is.Not.Null); |
| 364 | + Assert.That(e3Mapping.table, Is.EqualTo("Entity3")); |
| 365 | + |
| 366 | + var e3Bag = e3Mapping.Items.OfType<HbmIdbag>().FirstOrDefault(); |
| 367 | + Assert.That(e3Bag, Is.Not.Null); |
| 368 | + Assert.That(e3Bag.Table, Is.EqualTo("Entity3_Component")); |
| 369 | + } |
| 370 | + } |
| 371 | +} |
0 commit comments