Skip to content

Commit c1f7f48

Browse files
committed
NH-3793 - Correctly set ReferencedEntityName from keyManyToOneSchema in ClassCompositeIdBinder
1 parent e61db9e commit c1f7f48

File tree

5 files changed

+229
-9
lines changed

5 files changed

+229
-9
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3793
5+
{
6+
[Serializable]
7+
public class ParentEntityKey : IEquatable<ParentEntityKey>
8+
{
9+
public ParentEntityKey()
10+
{
11+
}
12+
13+
public virtual int Number { get; set; }
14+
public virtual string StringKey { get; set; }
15+
16+
public bool Equals(ParentEntityKey other)
17+
{
18+
if (other == null)
19+
{
20+
return false;
21+
}
22+
return Number.Equals(other.Number) &&
23+
(StringKey ?? String.Empty).Equals(other.StringKey ?? String.Empty);
24+
}
25+
26+
public override bool Equals(object obj)
27+
{
28+
return Equals(obj as ParentEntityKey);
29+
}
30+
31+
public override int GetHashCode()
32+
{
33+
unchecked
34+
{
35+
return (Number * 19) + (StringKey ?? String.Empty).GetHashCode();
36+
}
37+
}
38+
}
39+
40+
[Serializable]
41+
public class ParentEntity
42+
{
43+
public ParentEntity()
44+
{
45+
}
46+
47+
public virtual ParentEntityKey ParentEntityKey { get; set; }
48+
public virtual int Number { get; set; }
49+
public virtual string StringKey { get; set; }
50+
public virtual string OtherInformationOne { get; set; }
51+
public virtual string OtherInformationTwo { get; set; }
52+
public virtual ISet<ChildEntity> Children { get; set; }
53+
}
54+
55+
[Serializable]
56+
public class ChildEntityKey : IEquatable<ChildEntityKey>
57+
{
58+
public ChildEntityKey()
59+
{
60+
}
61+
62+
public virtual ParentEntity ParentEntity { get; set; }
63+
public virtual string ChildIdentifier { get; set; }
64+
65+
public virtual bool Equals(ChildEntityKey other)
66+
{
67+
if (other == null)
68+
{
69+
return false;
70+
}
71+
if (ParentEntity == null && other.ParentEntity == null)
72+
{
73+
return ChildIdentifier == other.ChildIdentifier;
74+
}
75+
if (ParentEntity == null || other.ParentEntity == null)
76+
{
77+
return false;
78+
}
79+
if (ParentEntity.ParentEntityKey == null && other.ParentEntity.ParentEntityKey == null)
80+
{
81+
return ChildIdentifier == other.ChildIdentifier;
82+
}
83+
if (ParentEntity.ParentEntityKey == null || other.ParentEntity.ParentEntityKey == null)
84+
{
85+
return false;
86+
}
87+
return ParentEntity.ParentEntityKey.Equals(other.ParentEntity.ParentEntityKey) &&
88+
ChildIdentifier == other.ChildIdentifier;
89+
}
90+
91+
public override bool Equals(object obj)
92+
{
93+
if (obj == null)
94+
{
95+
return false;
96+
}
97+
var other = obj as ChildEntityKey;
98+
if (other != null)
99+
{
100+
return Equals(other);
101+
}
102+
return base.Equals(obj);
103+
}
104+
105+
public override int GetHashCode()
106+
{
107+
if (ChildIdentifier == null)
108+
{
109+
return Int32.MinValue;
110+
}
111+
return ChildIdentifier.GetHashCode();
112+
}
113+
}
114+
115+
[Serializable]
116+
public class ChildEntity
117+
{
118+
public ChildEntity()
119+
{
120+
}
121+
122+
public virtual ChildEntityKey ChildEntityKey { get; set; }
123+
public virtual string ChildIdentifier { get; set; }
124+
public virtual string SomeOtherProperty { get; set; }
125+
}
126+
127+
[Serializable]
128+
public class ObjectWithBothEntityNames
129+
{
130+
public ObjectWithBothEntityNames()
131+
{
132+
}
133+
134+
public virtual int ObjectWithBothEntityNamesId { get; set; }
135+
136+
public virtual ISet<ParentEntity> ParentEntityOneSet { get; set; }
137+
public virtual ISet<ParentEntity> ParentEntityTwoSet { get; set; }
138+
}
139+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3793
4+
{
5+
[TestFixture]
6+
public class Fixture
7+
{
8+
[Test]
9+
public void CanCreateSchemaWithCompositeIdWithKeyManyToOneUsesEntityName()
10+
{
11+
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
12+
cfg.AddResource("NHibernate.Test.NHSpecificTest.NH3793.Mappings.hbm.xml", GetType().Assembly);
13+
Assert.DoesNotThrow(() => cfg.BuildMappings());
14+
}
15+
}
16+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3793">
3+
4+
<class name="ParentEntity" entity-name="ParentEntityOne" table="AAPARN">
5+
<composite-id name="ParentEntityKey">
6+
<key-property name="Number" column="AAPNUM" />
7+
<key-property name="StringKey" column="AAPKEY" />
8+
</composite-id>
9+
<property name="OtherInformationOne" column="AAOINF" />
10+
<set name="Children" inverse="true">
11+
<key>
12+
<column name="BBPNUM" />
13+
<column name="BBPKEY" />
14+
</key>
15+
<one-to-many class="ChildEntity" entity-name="ChildEntityOne"/>
16+
</set>
17+
</class>
18+
19+
<class name="ChildEntity" entity-name="ChildEntityOne" table="AACHLD">
20+
<composite-id name="ChildEntityKey">
21+
<key-many-to-one name="ParentEntity" entity-name="ParentEntityOne">
22+
<column name="AAPNUM" />
23+
<column name="AAPKEY" />
24+
</key-many-to-one>
25+
<key-property name="ChildIdentifier" column="BBCHID" />
26+
</composite-id>
27+
<property name="ChildIdentifier" column="BBCHID" />
28+
<property name="SomeOtherProperty" column="BBZZZZ" />
29+
</class>
30+
31+
<class name="ParentEntity" entity-name="ParentEntityTwo" table="CCPARN">
32+
<composite-id name="ParentEntityKey">
33+
<key-property name="Number" column="AAPNUM" />
34+
<key-property name="StringKey" column="AAPKEY" />
35+
</composite-id>
36+
<property name="OtherInformationTwo" column="AAOINF" />
37+
<set name="Children" inverse="true">
38+
<key>
39+
<column name="BBPNUM" />
40+
<column name="BBPKEY" />
41+
</key>
42+
<one-to-many class="ChildEntity" entity-name="ChildEntityTwo"/>
43+
</set>
44+
</class>
45+
46+
<class name="ChildEntity" entity-name="ChildEntityTwo" table="CCCHLD">
47+
<composite-id name="ChildEntityKey">
48+
<key-many-to-one name="ParentEntity" class="ParentEntity" entity-name="ParentEntityTwo">
49+
<column name="AAPNUM" />
50+
<column name="AAPKEY" />
51+
</key-many-to-one>
52+
<key-property name="ChildIdentifier" column="BBCHID" />
53+
</composite-id>
54+
<property name="ChildIdentifier" column="BBCHID" />
55+
<property name="SomeOtherProperty" column="BBZZZZ" />
56+
</class>
57+
58+
<class name="ObjectWithBothEntityNames" table="OWBOTH">
59+
<id name="ObjectWithBothEntityNamesId" column="OWBTID" generator="assigned" />
60+
<set name="ParentEntityOneSet">
61+
<key column="AAPNUM" />
62+
<one-to-many class="ParentEntity" entity-name="ParentEntityOne"/>
63+
</set>
64+
<set name="ParentEntityTwoSet">
65+
<key column="AAPTNM" />
66+
<one-to-many class="ParentEntity" entity-name="ParentEntityTwo"/>
67+
</set>
68+
</class>
69+
70+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@
785785
<Compile Include="NHSpecificTest\NH3100\ByCodeFixture.cs" />
786786
<Compile Include="NHSpecificTest\NH3817\Entity.cs" />
787787
<Compile Include="NHSpecificTest\NH3817\Fixture.cs" />
788+
<Compile Include="NHSpecificTest\NH3793\Entity.cs" />
789+
<Compile Include="NHSpecificTest\NH3793\Fixture.cs" />
788790
<Compile Include="NHSpecificTest\NH2839\FixtureByCode.cs" />
789791
<Compile Include="NHSpecificTest\NH3564\FixtureByCode.cs" />
790792
<Compile Include="NHSpecificTest\NH3583\Entity.cs" />
@@ -3252,6 +3254,7 @@
32523254
<EmbeddedResource Include="NHSpecificTest\NH3609\Mappings.hbm.xml" />
32533255
<EmbeddedResource Include="NHSpecificTest\NH3818\Mappings.hbm.xml" />
32543256
<EmbeddedResource Include="NHSpecificTest\NH3817\Mappings.hbm.xml" />
3257+
<EmbeddedResource Include="NHSpecificTest\NH3793\Mappings.hbm.xml" />
32553258
<EmbeddedResource Include="NHSpecificTest\NH3666\Mappings.hbm.xml">
32563259
<SubType>Designer</SubType>
32573260
</EmbeddedResource>

src/NHibernate/Cfg/XmlHbmBinding/ClassCompositeIdBinder.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,7 @@ private void BindManyToOne(HbmKeyManyToOne keyManyToOneSchema, ManyToOne manyToO
173173
? manyToOne.IsLazy
174174
: keyManyToOneSchema.lazy == HbmRestrictedLaziness.Proxy;
175175

176-
string typeNode = keyManyToOneSchema.@class;
177-
if (typeNode != null)
178-
{
179-
manyToOne.ReferencedEntityName = GetClassName(typeNode, mappings);
180-
}
181-
else
182-
{
183-
manyToOne.ReferencedEntityName = null;
184-
}
176+
manyToOne.ReferencedEntityName = GetEntityName(keyManyToOneSchema, mappings);
185177

186178
manyToOne.IsIgnoreNotFound = false;
187179

0 commit comments

Comments
 (0)