|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using NHibernate; |
| 4 | +using NHibernate.Cfg.MappingSchema; |
| 5 | +using NHibernate.Linq; |
| 6 | +using NHibernate.Mapping.ByCode; |
| 7 | +using NHibernate.Mapping.ByCode.Conformist; |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace NHibernate.Test.MappingByCode.ExplicitlyDeclaredModelTests |
| 11 | +{ |
| 12 | + public abstract class Parent |
| 13 | + { |
| 14 | + public virtual ParentId Id { get; set; } |
| 15 | + public virtual string Name { get; set; } |
| 16 | + public virtual Address Address { get; set; } |
| 17 | + } |
| 18 | + |
| 19 | + public class ParentId |
| 20 | + { |
| 21 | + public string Key1 { get; set; } |
| 22 | + public string Key2 { get; set; } |
| 23 | + |
| 24 | + public override bool Equals(object obj) |
| 25 | + { |
| 26 | + var pk = obj as ParentId; |
| 27 | + |
| 28 | + if (obj == null) |
| 29 | + return false; |
| 30 | + |
| 31 | + return (Key1 == pk.Key1 && Key2 == pk.Key2); |
| 32 | + } |
| 33 | + |
| 34 | + public override int GetHashCode() |
| 35 | + { |
| 36 | + return (Key1 + "|" + Key2).GetHashCode(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public class Child1 : Parent |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + public class Child2 : Parent |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + class Child1Map : ClassMapping<Child1> |
| 49 | + { |
| 50 | + public Child1Map() |
| 51 | + { |
| 52 | + Table("Child1"); |
| 53 | + |
| 54 | + ComponentAsId<ParentId>(x => x.Id, pk => |
| 55 | + { |
| 56 | + pk.Property(x => x.Key1, x => x.Column("key1")); |
| 57 | + pk.Property(x => x.Key2, x => |
| 58 | + { |
| 59 | + x.Column("key2"); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + Property(x => x.Name); |
| 64 | + |
| 65 | + Component<Address>(x => x.Address, map => |
| 66 | + { |
| 67 | + map.Property(y => y.City, mc => mc.Column("city1")); |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + class Child2Map : ClassMapping<Child2> |
| 73 | + { |
| 74 | + public Child2Map() |
| 75 | + { |
| 76 | + Table("Child2"); |
| 77 | + |
| 78 | + ComponentAsId<ParentId>(x => x.Id, pk => |
| 79 | + { |
| 80 | + pk.Property(x => x.Key1, x => x.Column("key1")); |
| 81 | + pk.Property(x => x.Key2, x => |
| 82 | + { |
| 83 | + x.Column("key2__"); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + Property(x => x.Name); |
| 88 | + |
| 89 | + Component<Address>(x => x.Address, map => |
| 90 | + { |
| 91 | + map.Property(y => y.City, mc => mc.Column("city2")); |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public class Address |
| 97 | + { |
| 98 | + public virtual string City { get; set; } |
| 99 | + } |
| 100 | + |
| 101 | + [TestFixture] |
| 102 | + public class ComponentAsIdTest |
| 103 | + { |
| 104 | + [Test] |
| 105 | + public void CanHaveSameComponentAsIdMultipleTimesWithDifferentColumnNamesForSameProperty() |
| 106 | + { |
| 107 | + //NH-3650 |
| 108 | + var model = new ModelMapper(); |
| 109 | + model.AddMapping<Child1Map>(); |
| 110 | + model.AddMapping<Child2Map>(); |
| 111 | + |
| 112 | + var mappings = model.CompileMappingForEach(new[] { typeof(Child1), typeof(Child2) }); |
| 113 | + |
| 114 | + var child1Mapping = mappings.ElementAt(0); |
| 115 | + Assert.AreEqual("city1", child1Mapping.RootClasses[0].Properties.OfType<HbmComponent>().First().Properties.OfType<HbmProperty>().Single().column); |
| 116 | + //next one fails |
| 117 | + Assert.AreEqual("key2", child1Mapping.RootClasses[0].CompositeId.Items.OfType<HbmKeyProperty>().Last().column1); |
| 118 | + |
| 119 | + var child2Mapping = mappings.ElementAt(1); |
| 120 | + Assert.AreEqual("city2", child2Mapping.RootClasses[0].Properties.OfType<HbmComponent>().First().Properties.OfType<HbmProperty>().Single().column); |
| 121 | + Assert.AreEqual("key2__", child2Mapping.RootClasses[0].CompositeId.Items.OfType<HbmKeyProperty>().Last().column1); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments