2016-05-24 25 views
0

我很新的NHibernate和我有這個問題,我需要從擴展表中的兩列出現在我的NHibernate映射<joined-subclass>,但我有最難找到適當的實施。NHibernate在<joined-subclass>或替代

下面是我的實現的簡化版本,我最初想到的將是實現我所需要的方式,但NHibernate不允許在<joined-subclass>內使用<join>

<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,但它並沒有被證明對這個困境非常有用。

任何可以指向我的提示或資源將不勝感激。

謝謝。

+0

只是旁註:不要使用組合鍵,除非你不能改變數據庫模型。它僅支持這種情況。它有很多退步,當你可以避開它時不值得一提。 –

回答

0

您可以:

  • 變化的類模型有另一個類的擴展,並將其映射爲一個對一個。
  • 更改數據庫以將值存儲在SpecialProgramAssociation中
  • 使用子類映射而不是連接的子類並加入到SpecialProgramAssociation表並從那裏到擴展名。這也需要改變數據庫模型,因爲你需要一個鑑別器。
+0

謝謝你的回答。 將評估這些選項。 更改數據庫模型可能被證明是最困難的,因爲架構是由第三方創建的公共平臺的一部分。 –