流畅的NHibernate - 具有共享引用的子类

时间:2010-02-04 23:01:43

标签: fluent-nhibernate reference shared subclass

编辑:更改了班级名称。

我正在使用Fluent NHibernate(v 1.0.0.614)自动化以下一组类(其中Entity是S#arp架构框架中提供的基类):

public class Car : Entity
{
    public virtual int ModelYear { get; set; }
    public virtual Company Manufacturer { get; set; }
}

public class Sedan : Car
{
    public virtual bool WonSedanOfYear { get; set; }
}

public class Company : Entity
{
    public virtual IList<Sedan> Sedans { get; set; }
}

这导致以下配置(写入hbm.xml):

   <class name="Company" table="Companies">
     <id name="Id" type="System.Int32" unsaved-value="0">
       <column name="`ID`" />
       <generator class="identity" />
     </id>
     <bag cascade="all" inverse="true" name="Sedans" mutable="true">
       <key>
           <column name="`CompanyID`" />
       </key>
       <one-to-many class="Sedan" />
     </bag>
   </class>

   <class name="Car" table="Cars">
     <id name="Id" type="System.Int32" unsaved-value="0">
       <column name="`ID`" />
       <generator class="identity" />
     </id>
     <property name="ModelYear" type="System.Int32">
       <column name="`ModelYear`" />
     </property>
     <many-to-one cascade="save-update" class="Company" name="Manufacturer">
       <column name="`CompanyID`" />
     </many-to-one>
     <joined-subclass name="Sedan">
       <key>
          <column name="`CarID`" />
       </key>
       <property name="WonSedanOfYear" type="System.Boolean">
          <column name="`WonSedanOfYear`" />
       </property>
     </joined-subclass>
   </class>

到目前为止一切顺利!但现在是丑陋的部分。生成的数据库表:

Table: Companies
Columns: ID (PK, int, not null)

Table: Cars
Columns: ID (PK, int, not null)
         ModelYear (int, null)
         CompanyID (FK, int, null)


Table: Sedan
Columns: CarID (PK, FK, int, not null)
         WonSedanOfYear (bit, null)
         CompanyID (FK, int, null)

而不是公司的一个FK,我得到两个!

如何确保我只为公司获得一个FK?覆盖自动化?制定一个公约?或者这是一个错误?感谢您的想法。

1 个答案:

答案 0 :(得分:0)

我感到痛苦。

我现在做了以下事情,因为我的时间不多了

public class JoinedSubclassConvention : IJoinedSubclassConvention
{
    public void Apply(IJoinedSubclassInstance instance)
    {
        switch (instance.EntityType.Name)
        {
            case "Business":
                instance.Key.ForeignKey("FK_Business_Customer");
                break;
            case "Person":
                instance.Key.ForeignKey("FK_Person_Customer");
                break;
            case "StaffMember":
                instance.Key.ForeignKey("FK_StaffMember_Customer");
                break;
        }
    }
}