我需要在多个类中重用组件类型,但该组件类型的映射将根据它所在的根类而有所不同。我们使用ByCode映射API来实现此目的。
看起来应该很简单:只需为每个类显式地映射组件。
但是NHibernate 4.0.4.4000下面的测试失败了:
[TestFixture]
public class DuplicateComponentMappingTests
{
[Test]
public void TopLevelComponentsAreSpecialisedForRootEntity()
{
var cfg = ConfigureWithMappings(m => {
m.Class<RootA>(r => {
r.Id(x => x.Id);
r.Component(x => x.Component, c => {
c.Property(x => x.PropertyA);
});
});
m.Class<RootB>(r => {
r.Id(x => x.Id);
r.Component(x => x.Component, c => {
c.Property(x => x.PropertyB);
});
});
});
using (var sessionFactory = cfg.BuildSessionFactory())
{
using (var session = sessionFactory.OpenSession())
{
session.CreateSQLQuery("create table RootA ( Id int, PropertyA int )").ExecuteUpdate();
session.CreateSQLQuery("create table RootB ( Id int, PropertyB int )").ExecuteUpdate();
}
using (var session = sessionFactory.OpenSession())
{
session.Save(new RootA { Component = new Component { PropertyA = 1 } }); // Fails because there's no PropertyB column, even though we didn't map it for RootA.
session.Flush();
}
}
}
private Configuration ConfigureWithMappings(Action<ModelMapper> map)
{
...
}
}
class RootA
{
public virtual int Id { get; set; }
public virtual Component Component { get; set; }
}
class RootB
{
public virtual int Id { get; set; }
public virtual Component Component { get; set; }
}
class Component
{
public virtual int PropertyA { get; set; }
public virtual int PropertyB { get; set; }
}
更糟糕的是,我们的实际组件类本身包含组件本身,大约有4个级别,它们还需要通过根实体来改变它们的映射。
我尝试的事情:
答案 0 :(得分:0)
追溯到一对已知的错误,自3.2 / 3.3以来:
所以现在的解决方案似乎是“不要那么做”。一旦我完全理解了问题,我会尝试提交修复。