我正在尝试映射这些类:
public interface IBusinessObject
{
Guid Id { get; set; }
}
public class Product
{
public virtual Guid Id { get; set; }
public virtual int ProductTypeId { get; set; }
}
public class ProductWeSell : Product, IBusinessObject
{
}
public class ProductWeDontSell : Product
{
}
带有2个表的数据库:
[BusinessObject] COLUMNS ([Id])
[Product] COLUMNS ([Id], [ProdyctTypeId])
我希望为BusinessObject提供Type-Per-Class,并为Product提供Type-Per-Class。这应该导致这种行为:
逻辑上hbm映射应该是:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="IBusinessObject" table="BusinessObject">
<joined-subclass name="ProductWeSell" table="Product"/>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" discriminator-value="null" name="Product" table="Product">
<discriminator type="String">
<column name="ProductTypeId" not-null="false" />
</discriminator>
<subclass name="ProductWeDontSell" discriminator-value="2" />
<subclass name="ProductWeSell" discriminator-value="1" />
</class>
</hibernate-mapping>
但我得到{“重复类/实体映射ProductWeSell”}错误。
答案 0 :(得分:1)
我很确定使用Fluent NHibernate无法做到这一点。我在尝试提出方法时创建了以下TestFixture。另外我不确定你要映射的是什么。如果您要将它用于系统中的每个实体,那么IBusinessObject似乎是多余的(CreateQuery(“from object”)将返回从对象继承的每个实体,即所有内容)
注意:目前,IBusinessObject查询失败(因为它未映射)。我不太清楚为什么没有映射IBusinessObject接口就行不通,因为隐式多态似乎应该覆盖它(参见:http://nhibernate.info/doc/nh/en/index.html#inheritace-mixingpolymorphism)。
[TestFixture]
public class TestFixture
{
private ISessionFactory _sessionFactory;
private ISession _session;
[SetUp]
public void Setup()
{
var fluentConfig = Fluently.Configure().Database(() => SQLiteConfiguration.Standard.InMemory().Provider<TestConnectionProvider>())
.Mappings(x=>x.FluentMappings.Add<ProductMap>()
.Add<ProductWeSellMap>()
.Add<ProductWeDontSellMap>());
var nhConfig = fluentConfig.BuildConfiguration();
_sessionFactory = fluentConfig.BuildSessionFactory();
var schema = new SchemaExport(nhConfig);
schema.Execute(false, true, false);
_session = _sessionFactory.OpenSession();
}
[Test]
public void SomeTest()
{
using (var itx = this._session.BeginTransaction())
{
var productSold = new ProductWeSell();
var productNotSold = new ProductWeDontSell();
_session.Save(productNotSold);
_session.Save(productSold);
itx.Commit();
}
using (var itx = this._session.BeginTransaction())
{
Assert.That(_session.CreateQuery("from ProductWeSell").List(), Has.Count.EqualTo(1));
Assert.That(_session.CreateQuery("from ProductWeDontSell").List(), Has.Count.EqualTo(1));
Assert.That(_session.CreateQuery("from IBusinessObject").List(), Has.Count.EqualTo(1));
Assert.That(_session.CreateQuery("from Product").List(), Has.Count.EqualTo(2));
}
}
}
public interface IBusinessObject
{
Guid Id { get; set; }
}
public class Product
{
public virtual Guid Id { get; set; }
public virtual int ProductTypeId { get; set; }
}
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
this.Id(x => x.Id);
this.DiscriminateSubClassesOnColumn("ProductTypeId");
}
}
public class ProductWeSell : Product, IBusinessObject
{
public const int ProductWeSellTypeId = 1;
}
public class ProductWeSellMap : SubclassMap<ProductWeSell>
{
public ProductWeSellMap()
{
this.DiscriminatorValue(ProductWeSell.ProductWeSellTypeId);
}
}
public class ProductWeDontSell : Product
{
public const int ProductWeDontSellTypeId = 2;
}
public class ProductWeDontSellMap : SubclassMap<ProductWeDontSell>
{
public ProductWeDontSellMap()
{
this.DiscriminatorValue(ProductWeDontSell.ProductWeDontSellTypeId);
}
}
这会产生以下HBM映射:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="CellTester.Test.Database.Product, Test, Version=2.0.0.3, Culture=neutral, PublicKeyToken=a15dc1b99998d28b" table="`Product`">
<id name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="guid.comb" />
</id>
<discriminator type="String">
<column name="ProductTypeId" />
</discriminator>
<subclass name="ProductWeSell, Test, Version=2.0.0.3, Culture=neutral, PublicKeyToken=a15dc1b99998d28b" discriminator-value="1" />
<subclass name="ProductWeDontSell, Test, Version=2.0.0.3, Culture=neutral, PublicKeyToken=a15dc1b99998d28b" discriminator-value="2" />
</class>
</hibernate-mapping>
答案 1 :(得分:0)
我得出了有趣的结论。我决定改变我的架构,并为数据访问层和域UI层使用单独的类,我只是在跨越图层时转换我的对象。无论如何,将数据访问和UI分离是一种很好的做法。所以我的数据访问模型现在很简单,没有任何继承,继承只出现在域-UI级别。