Skip to content

NH-3527 - UnionSubclassMapper should mark an abstract type as abstract in the generated HbmMapping #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using SharpTestsEx;

namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3527
{
[TestFixture]
public class FixtureByCode : TestCaseMappingByCode
{
[Test]
public void VerifyMapping()
{
var mapping = this.GetMappings();

// Item mapping.
mapping.UnionSubclasses[0].abstractSpecified.Should().Be.True();
mapping.UnionSubclasses[0][email protected]().Be.True();
// InventoryItem mapping.
mapping.UnionSubclasses[1].abstractSpecified.Should().Be.False();
mapping.UnionSubclasses[1][email protected]().Be.False();
}

[Test]
public void VerifyTables()
{
using (var session = this.OpenSession())
{
var tableNameColumnName = "TABLE_NAME";
var itemTableName = "Item";
var inventoryItemTableName = "InventoryItem";

var schema = this.Dialect.GetDataBaseSchema((DbConnection) session.Connection);
var tables = schema.GetTables(null, null, null, null).AsEnumerable();
var itemTable = tables.SingleOrDefault(
x => string.Equals(x.Field<string>(tableNameColumnName),
itemTableName,
StringComparison.InvariantCultureIgnoreCase));
var inventoryItemTable = tables.SingleOrDefault(
x => string.Equals(x.Field<string>(tableNameColumnName),
inventoryItemTableName,
StringComparison.InvariantCultureIgnoreCase));

itemTable.Should().Be.Null();
inventoryItemTable.Should().Not.Be.Null();
}
}

protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is MsSql2008Dialect;
}

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EntityBase>(
m => m.Id(x => x.Id, im => im.Generator(Generators.HighLow)));
mapper.UnionSubclass<Item>(m => { });
mapper.UnionSubclass<InventoryItem>(m => { });

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
}
}
20 changes: 20 additions & 0 deletions src/NHibernate.Test/MappingByCode/IntegrationTests/NH3527/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Test.MappingByCode.IntegrationTests.NH3527
{
public abstract class EntityBase
{
public virtual int Id { get; set; }
}

public abstract class Item : EntityBase
{
}

public class InventoryItem : Item
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode.Impl;
using NUnit.Framework;
using SharpTestsEx;

namespace NHibernate.Test.MappingByCode.MappersTests.UnionSubclassMapperTests
{
[TestFixture]
public class AbstractAttributeTests
{
private abstract class EntityBase
{
}

private abstract class Item : EntityBase
{
}

private class InventoryItem : Item
{
}

[Test]
public void CanSetAbstractAttributeOnAbstractClass()
{
var mapping = new HbmMapping();
var mapper = new UnionSubclassMapper(typeof(Item), mapping);

mapping.UnionSubclasses[0].abstractSpecified.Should().Be.True();
mapping.UnionSubclasses[0][email protected]().Be.True();
}

[Test]
public void CanSetAbstractAttributeOnConcreteClass()
{
var mapping = new HbmMapping();
var mapper = new UnionSubclassMapper(typeof(InventoryItem), mapping);

mapping.UnionSubclasses[0].abstractSpecified.Should().Be.False();
mapping.UnionSubclasses[0][email protected]().Be.False();
}
}
}
6 changes: 6 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data.OracleClient" />
<Reference Include="System.Drawing" />
<Reference Include="System.Linq.Dynamic, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down Expand Up @@ -589,6 +590,10 @@
<Compile Include="MappingByCode\IntegrationTests\NH2825\Fixture.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2825\FixtureByCode.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH2825\Parent.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3280\Domain.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3280\OneToOneToInheritedProperty.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3527\Model.cs" />
<Compile Include="MappingByCode\IntegrationTests\NH3527\FixtureByCode.cs" />
<Compile Include="MappingByCode\MappersTests\AnyMapperTest.cs" />
<Compile Include="MappingByCode\MappersTests\IdMapperTest.cs" />
<Compile Include="MappingByCode\MappersTests\ManyToOneMapperTest.cs" />
Expand Down Expand Up @@ -625,6 +630,7 @@
<Compile Include="MappingByCode\MappersTests\SubclassMapperTests\SetPersisterTests.cs" />
<Compile Include="MappingByCode\MappersTests\SubclassMapperTests\TablesSincronizationTests.cs" />
<Compile Include="MappingByCode\MappersTests\SubclassMapperWithJoinPropertiesTest.cs" />
<Compile Include="MappingByCode\MappersTests\UnionSubclassMapperTests\AbstractAttributeTests.cs" />
<Compile Include="MappingByCode\MappersTests\UnionSubclassMapperTests\SetPersisterTests.cs" />
<Compile Include="MappingByCode\MappersTests\UnionSubclassMapperTests\TablesSincronizationTests.cs" />
<Compile Include="MappingByCode\MixAutomapping\ArrayCollectionTests.cs" />
Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/UnionSubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public UnionSubclassMapper(System.Type subClass, HbmMapping mapDoc)
var toAdd = new[] {classMapping};
classMapping.name = subClass.GetShortClassName(mapDoc);
classMapping.extends = subClass.BaseType.GetShortClassName(mapDoc);
if (subClass.IsAbstract)
{
classMapping.@abstract = true;
classMapping.abstractSpecified = true;
}
mapDoc.Items = mapDoc.Items == null ? toAdd : mapDoc.Items.Concat(toAdd).ToArray();
}

Expand Down