Skip to content

Commit 09e7b72

Browse files
EamonHethertonhazzik
authored andcommitted
Fix usage of parent class properties in ComponentAsId (NH-3105)
1 parent c4d22e8 commit 09e7b72

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.NH3105
7+
{
8+
[TestFixture]
9+
public class Fixture
10+
{
11+
[Test]
12+
public void CanMapComponentAsIdWhenComnponentIsDeclaredInBaseClass()
13+
{
14+
var mapper = new ModelMapper();
15+
mapper.Class<Entity>(rc =>
16+
{
17+
rc.ComponentAsId(x => x.ComponentId, m => m.Property(c => c.Id, pm => pm.Column("IdColumn")));
18+
});
19+
20+
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
21+
22+
var entity = mapping.RootClasses[0];
23+
var componentId = entity.CompositeId;
24+
Assert.That(componentId, Is.Not.Null);
25+
26+
var key = componentId.Items[0] as HbmKeyProperty;
27+
Assert.That(key.Columns.Single().name, Is.EqualTo("IdColumn"));
28+
}
29+
30+
31+
[Test]
32+
public void CanMapIdWhenIdIsDeclaredInBaseClass()
33+
{
34+
var mapper = new ModelMapper();
35+
mapper.Class<Entity>(rc =>
36+
{
37+
rc.Id(x => x.Id, m => m.Column("IdColumn"));
38+
});
39+
40+
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
41+
42+
var entity = mapping.RootClasses[0];
43+
var componentId = entity.CompositeId;
44+
Assert.That(componentId, Is.Null);
45+
46+
var id = entity.Id;
47+
Assert.That(id, Is.Not.Null);
48+
}
49+
50+
[Test]
51+
public void CanMapComponentAsIdWhenComnponentIsDeclaredInClass()
52+
{
53+
var mapper = new ModelMapper();
54+
mapper.Class<EntityBase>(rc =>
55+
{
56+
rc.ComponentAsId(x => x.ComponentId, m => m.Property(c => c.Id, pm => pm.Column("IdColumn")));
57+
});
58+
59+
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
60+
61+
var entity = mapping.RootClasses[0];
62+
var componentId = entity.CompositeId;
63+
Assert.That(componentId, Is.Not.Null);
64+
65+
var key = componentId.Items[0] as HbmKeyProperty;
66+
Assert.That(key.Columns.Single().name, Is.EqualTo("IdColumn"));
67+
}
68+
}
69+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3105
4+
{
5+
class EntityBase
6+
{
7+
public virtual EntityId ComponentId { get; set; }
8+
public virtual Guid Id { get; set; }
9+
}
10+
11+
class Entity : EntityBase
12+
{
13+
public virtual string Name { get; set; }
14+
}
15+
16+
class EntityId
17+
{
18+
public virtual Guid Id { get; set; }
19+
}
20+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,8 @@
10061006
<Compile Include="NHSpecificTest\NH3138\FixtureByCode.cs" />
10071007
<Compile Include="NHSpecificTest\NH3153\Model.cs" />
10081008
<Compile Include="NHSpecificTest\NH3153\Fixture.cs" />
1009+
<Compile Include="MappingByCode\IntegrationTests\NH3105\Model.cs" />
1010+
<Compile Include="MappingByCode\IntegrationTests\NH3105\Fixture.cs" />
10091011
<Compile Include="NHSpecificTest\NH941\Domain.cs" />
10101012
<Compile Include="NHSpecificTest\NH941\Fixture.cs" />
10111013
<Compile Include="NHSpecificTest\NH941\FixtureUsingList.cs" />

src/NHibernate/Mapping/ByCode/Impl/DefaultCandidatePersistentMembersProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public IEnumerable<MemberInfo> GetEntityMembersForPoid(System.Type entityClass)
1818
{
1919
return entityClass.IsInterface
2020
? entityClass.GetInterfaceProperties()
21-
: entityClass.GetProperties(RootClassPropertiesBindingFlags).Concat(GetFieldsOfHierarchy(entityClass));
21+
: entityClass.GetPropertiesOfHierarchy().Concat(GetFieldsOfHierarchy(entityClass));
2222
}
2323

2424
public IEnumerable<MemberInfo> GetRootEntityMembers(System.Type entityClass)

src/NHibernate/Mapping/ByCode/TypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ private static IEnumerable<MemberInfo> GetPropertiesOfInterfacesImplemented(this
546546
}
547547
}
548548

549-
private static IEnumerable<MemberInfo> GetPropertiesOfHierarchy(this System.Type type)
549+
internal static IEnumerable<MemberInfo> GetPropertiesOfHierarchy(this System.Type type)
550550
{
551551
if(type.IsInterface)
552552
{

0 commit comments

Comments
 (0)