0

采取以下实体:

public class Company : Entity<Guid>
{
    public virtual string Name { get; set; }
    public virtual IList<IEmployee> Employees { get; set; }

    public Company()
    {
        Id = Guid.NewGuid();
        Employees = new List<IEmployee>();
    }
}

public interface IEmployee
{
    Guid? Id { get; set; }
    string Name { get; set; }
    void Work();
    Company Company { get; set; }
}

public class ProductionEmployee : Entity<Guid>, IEmployee
{
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }

    public ProductionEmployee()
    {
        Id = Guid.NewGuid();
    }

    public virtual void Work()
    {
        Console.WriteLine("I'm making the stuff.");
    }
}

public class SalesEmployee : Entity<Guid>, IEmployee
{
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }

    public SalesEmployee()
    {
        Id = Guid.NewGuid();
    }

    public virtual void Work()
    {
        Console.WriteLine("I'm selling the stuff.");
    }
}

在 NHibernate 中以如下方式映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               namespace="PolymorphicUnionSubclass.Domain.Entities"
               assembly="PolymorphicUnionSubclass.Domain">
  <class name="Company" table="`Company`">
    <id name="Id" column="Id" type="guid">
      <generator class="assigned"/>
    </id>
    <property name="Name" column="`Name`"/>

    <bag name="Employees" inverse="true" cascade="save-update">
      <key column="CompanyId"></key>
      <one-to-many class="IEmployee" />
    </bag>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               namespace="PolymorphicUnionSubclass.Domain.Entities"
               assembly="PolymorphicUnionSubclass.Domain">
  <class name="IEmployee" abstract="true">
    <id name="Id" column="Id" type="guid">
      <generator class="assigned"/>
    </id>

    <many-to-one name="Company" column="`CompanyId`" cascade="save-update"/>

    <union-subclass name="ProductionEmployee" table ="`ProductionEmployee`" >
    </union-subclass>

    <union-subclass name="SalesEmployee" table ="`SalesEmployee`">
    </union-subclass>

  </class>
</hibernate-mapping>

如果我创建一个公司实体并将 IEmployee 实体添加到它的集合中(同时设置 IEmployee 实体的公司属性以创建双向关系),那么当我保存公司时,一切都按预期进入数据库。在 PoductionEmployee 和 SalesEmlpoyee 记录上正确设置了 companyId。

但是当我来加载它时,我收到以下错误:

为“employees0_”多次指定了“CompanyId”列

生成的 SQL 如下所示:

SELECT employees0_.CompanyId as CompanyId1_, employees0_.Id as Id1_, employees0_.Id as Id9_0_, employees0_.[CompanyId] as CompanyId2_9_0_, employees0_.clazz_ as clazz_0_ 
FROM ( select Id, CompanyId, CompanyId, 1 as clazz_ from [ProductionEmployee] union all select Id, CompanyId, CompanyId, 2 as clazz_ from [SalesEmployee] ) employees0_ 
WHERE employees0_.CompanyId=?

为什么它会生成两次 CopmanyId 列,我该如何防止这种情况?

4

1 回答 1

0

最终与联合子类无关。问题出在我指定 column="CompanyId" 的一对多集合中,而在我指定 column="`CompanyId`" 的多对一集合中。在一个而不是另一个中包含反引号导致 NHibernate 认为它们是不同的列。在我使用 NHibernate 的所有时间里,从来没有遇到过这种情况。

于 2013-06-24T12:23:51.520 回答