Skip to content

Commit f577ec5

Browse files
diegosehazzik
authored andcommitted
NH-3140 - Fix incorrect mapping-by-code of many-to-many collection of elements when a column name matches a class name
1 parent e19592b commit f577ec5

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3140
5+
{
6+
public class Foo
7+
{
8+
public virtual Guid Id { get; set; }
9+
public virtual ICollection<Bar> Bars { get; set; }
10+
}
11+
12+
public class Bar
13+
{
14+
public virtual Guid Id { get; set; }
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.NH3140
7+
{
8+
public class Fixture
9+
{
10+
[TestCase("DifferentFromBar")]
11+
[TestCase("Bar")]
12+
public void ExplicitColumnNameIsAlwaysMapped(string columnName)
13+
{
14+
var mapper = new ModelMapper();
15+
mapper.Class<Foo>(cm => cm.Bag(x => x.Bars,
16+
bpm => { },
17+
cer => cer.ManyToMany(mtmm => mtmm.Column(columnName))));
18+
var mapping = mapper.CompileMappingFor(new[] { typeof(Foo), typeof(Bar) });
19+
var hbmClass = mapping.RootClasses.Single(x => x.Name == "Foo");
20+
var hbmBag = hbmClass.Properties.OfType<HbmBag>().Single();
21+
var hbmManyToMany = (HbmManyToMany)hbmBag.ElementRelationship;
22+
Assert.AreEqual(columnName, hbmManyToMany.column);
23+
}
24+
}
25+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@
673673
<Compile Include="NHSpecificTest\NH2923\Models.cs" />
674674
<Compile Include="NHSpecificTest\NH2923\ExtraLazyFixture.cs" />
675675
<Compile Include="NHSpecificTest\NH3405\Fixture.cs" />
676+
<Compile Include="MappingByCode\IntegrationTests\NH3140\Domain.cs" />
677+
<Compile Include="MappingByCode\IntegrationTests\NH3140\Fixture.cs" />
676678
<Compile Include="NHSpecificTest\NH3428\Entity.cs" />
677679
<Compile Include="NHSpecificTest\NH3428\Fixture.cs" />
678680
<Compile Include="NHSpecificTest\NH3408\Fixture.cs" />

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@ public void Column(Action<IColumnMapper> columnMapper)
4444
unique = manyToMany.unique,
4545
uniqueSpecified = manyToMany.unique,
4646
};
47-
string defaultColumnName = elementType.Name;
48-
columnMapper(new ColumnMapper(hbm, defaultColumnName));
47+
columnMapper(new ColumnMapper(hbm, Collection.DefaultElementColumnName));
4948
if (ColumnTagIsRequired(hbm))
5049
{
5150
manyToMany.Items = new[] {hbm};
5251
ResetColumnPlainValues();
5352
}
5453
else
5554
{
56-
manyToMany.column = defaultColumnName == null || !defaultColumnName.Equals(hbm.name) ? hbm.name : null;
55+
manyToMany.column = Collection.DefaultElementColumnName.Equals(hbm.name) ? null : hbm.name;
5756
manyToMany.unique = hbm.unique;
5857
}
5958
}

0 commit comments

Comments
 (0)