<joined-subclass>或Alternative

时间:2016-05-24 19:04:34

标签: c# nhibernate joined-subclass

我是NHibernate的新手,我遇到这个问题,我需要扩展表中的两列出现在我的NHibernate映射中的<joined-subclass>上,但是我最难找到合适的实施

下面是我的实现的简化版本,我最初认为是完成我需要的方法,但NHibernate不允许在<join>内使用<joined-subclass>

  <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                     assembly="Sample.NHibernate"
                     namespace="Sample.NHibernate.ProgramAssociationAggregate"
                     default-access="property">

  <!-- Class definition -->
  <class name="ProgramAssociation" table="ProgramAssociation" lazy="false">

    <!-- Composite primary key -->
    <composite-id>
      <key-property name="BeginDate" column="BeginDate" type="date" />
      <key-property name="OrganizationId" column="OrganizationId" type="int" />
      <key-property name="ProgramName" column="ProgramName" type="string" length="60" />
      <key-property name="ProgramTypeId" column="ProgramTypeId" type="int" />
      <key-property name="PersonId" column="PersonId" type="int" />
    </composite-id>

    <!-- Optimistic locking for aggregate root -->
    <version name="LastModifiedDate" column="LastModifiedDate" type="timestamp" />

    <!-- Transient state detection -->
    <property name="CreateDate" column="CreateDate" type="DateTime" not-null="true" />

    <!-- Unique Guid-based identifier for aggregate root -->
    <property name="Id" column="Id" type="guid" not-null="true" />

    <!-- Properties -->
    <property name="EndDate" column="EndDate" type="date" />

    <!-- Derived classes -->
    <joined-subclass name="Sample.NHibernate.ProgramAssociationAggregate.SpecialProgramAssociation" table="SpecialProgramAssociation" lazy="false">
      <key>
        <column name="BeginDate" />
        <column name="OrganizationId" />
        <column name="ProgramName" />
        <column name="ProgramTypeId" />
        <column name="PersonId" />
      </key>

      <!-- PK properties -->
      <property name="PersonId" column="PersonId" type="int" not-null="true" insert="false" />
      <property name="ProgramTypeId" column="ProgramTypeId" type="int" not-null="true" insert="false" />
      <property name="BeginDate" column="BeginDate" type="date" not-null="true" insert="false" />
      <property name="ProgramName" column="ProgramName" type="string" length="60" not-null="true" insert="false" />
      <property name="OrganizationId" column="OrganizationId" type="int" not-null="true" insert="false" />

      <!-- Properties -->
      <property name="IEPReviewDate" column="IEPReviewDate" type="date" />
      <property name="IEPBeginDate" column="IEPBeginDate" type="date" />
      <property name="IEPEndDate" column="IEPEndDate" type="date" />

      <!-- Trying to join this table, but NHibernate does not allow for this. What is the best way to accomplish essentially the same thing? -->
      <join table="SpecialProgramAssociationExtension" schema="extension">
        <key>
          <column name="BeginDate" />
          <column name="OrganizationId" />
          <column name="ProgramName" />
          <column name="ProgramTypeId" />
          <column name="PersonId" />
        </key>
        <property name="IEPEventCode"/>
        <property name="WrittenConsentDate" type="date"/>
      </join>

    </joined-subclass>

  </class>
</hibernate-mapping>

对NHibernate有更多经验的人是否知道如何完成我需要的方法?

我最近一直在这里使用this NHibernate resource,但事实证明这对于这种困境并不是很有用。

我可以非常感谢任何可以指向我的提示或资源。

谢谢。

1 个答案:

答案 0 :(得分:0)

你可以:

  • 将类模型更改为具有扩展的另一个类,并将其映射为一对一。
  • 更改数据库以将值存储在SpecialProgramAssociation
  • 使用子类映射而不是连接子类并连接到SpecialProgramAssociation表并从那里连接到扩展。这也需要更改数据库模型,因为您需要一个鉴别器。