Skip to content

Commit 809cce0

Browse files
committed
NH-3049 - Fix mapping to fields
1 parent 71994bd commit 809cce0

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

src/NHibernate.Test/App.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,13 @@
103103
</log4net>
104104

105105
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
106+
<runtime>
107+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
108+
<dependentAssembly>
109+
<assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral"/>
110+
<bindingRedirect oldVersion="0.0.0.0-2.6.1.12217" newVersion="2.6.1.12217"/>
111+
</dependentAssembly>
112+
</assemblyBinding>
113+
</runtime>
106114

107115
</configuration>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Mapping.ByCode;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.MappingByCode.ExplicitMappingTests
7+
{
8+
[TestFixture]
9+
public class MappingOfInternalMembersOnRootEntity
10+
{
11+
public class MyClass
12+
{
13+
protected internal int _id;
14+
protected internal int _version;
15+
protected internal string _something;
16+
}
17+
18+
[Test]
19+
public void MapClassWithInternalIdAndProperty()
20+
{
21+
var mapper = new ModelMapper();
22+
mapper.Class<MyClass>(ca =>
23+
{
24+
ca.Id(x => x._id, map =>
25+
{
26+
map.Column("MyClassId");
27+
map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
28+
});
29+
ca.Version(x => x._version, map => { });
30+
ca.Property(x => x._something, map => map.Length(150));
31+
});
32+
var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
33+
var hbmClass = hbmMapping.RootClasses[0];
34+
Assert.That(hbmClass, Is.Not.Null);
35+
36+
var hbmId = hbmClass.Id;
37+
Assert.That(hbmId, Is.Not.Null);
38+
Assert.That(hbmId.name, Is.EqualTo("_id"));
39+
Assert.That(hbmId.access, Is.EqualTo("field"));
40+
41+
var hbmIdGenerator = hbmId.generator;
42+
Assert.That(hbmIdGenerator, Is.Not.Null);
43+
Assert.That(hbmIdGenerator.@class, Is.EqualTo("hilo"));
44+
Assert.That(hbmIdGenerator.param[0].name, Is.EqualTo("max_low"));
45+
Assert.That(hbmIdGenerator.param[0].GetText(), Is.EqualTo("100"));
46+
47+
var hbmVersion = hbmClass.Version;
48+
Assert.That(hbmVersion, Is.Not.Null);
49+
Assert.That(hbmVersion.name, Is.EqualTo("_version"));
50+
51+
var hbmProperty = hbmClass.Properties.OfType<HbmProperty>().Single();
52+
Assert.That(hbmProperty.name, Is.EqualTo("_something"));
53+
Assert.That(hbmProperty.access, Is.EqualTo("field"));
54+
Assert.That(hbmProperty.length, Is.EqualTo("150"));
55+
}
56+
}
57+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@
582582
<Compile Include="MappingByCode\ExplicitMappingTests\ConformistMappingRegistrationTests\UnionSubclassMappingRegistrationTest.cs" />
583583
<Compile Include="MappingByCode\ExplicitMappingTests\DynamicComponentMappingTests.cs" />
584584
<Compile Include="MappingByCode\ExplicitMappingTests\IdBagMappingTest.cs" />
585+
<Compile Include="MappingByCode\ExplicitMappingTests\MappingOfInternalMembersOnRootEntity.cs" />
585586
<Compile Include="MappingByCode\ExplicitMappingTests\MappingOfPrivateMembersOnRootEntity.cs" />
586587
<Compile Include="MappingByCode\ExplicitMappingTests\NaturalIdTests.cs" />
587588
<Compile Include="MappingByCode\ExplicitMappingTests\NestedComponetsTests.cs" />

src/NHibernate/Mapping/ByCode/TypeExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,16 @@ public static MemberInfo DecodeMemberAccessExpressionOf<TEntity, TProperty>(Expr
100100
return memberOfDeclaringType;
101101
}
102102

103-
return typeof (TEntity).GetProperty(memberOfDeclaringType.Name, PropertiesOfClassHierarchy,
104-
null, memberOfDeclaringType.GetPropertyOrFieldType(), new System.Type[0], null);
103+
var propertyInfo = memberOfDeclaringType as PropertyInfo;
104+
if (propertyInfo != null)
105+
{
106+
return typeof (TEntity).GetProperty(propertyInfo.Name, PropertiesOfClassHierarchy, null, propertyInfo.PropertyType, new System.Type[0], null);
107+
}
108+
if (memberOfDeclaringType is FieldInfo)
109+
{
110+
return typeof (TEntity).GetField(memberOfDeclaringType.Name, PropertiesOfClassHierarchy);
111+
}
112+
throw new NotSupportedException();
105113
}
106114

107115
public static MemberInfo GetMemberFromDeclaringType(this MemberInfo source)

0 commit comments

Comments
 (0)