简体   繁体   中英

NHibernate <Join> within <joined-subclass> or Alternative

I'm pretty new to NHibernate and I have this problem where I need two columns from an extension table to be present on a <joined-subclass> in my NHibernate mapping, but I'm having the hardest time finding the appropriate implementation.

Below is a simplified version of my implementation and what I initially had thought would be the way to accomplish what I'm needing, but NHibernate does not allow a <join> within a <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>

Does anyone with more experience with NHibernate know of a way to accomplish what I'm needing?

I've been using this NHibernate resource here lately, but it hasn't proved to be very useful for this predicament.

Any tips or resources one could point me to would be greatly appreciated.

Thank you.

You could:

  • change the class model to have another class for the extension and map it as a one-to-one.
  • change the database to store the values in the SpecialProgramAssociation
  • use subclass mapping instead of joined subclass and join to the SpecialProgramAssociation table and from there to the extension. This requires changing the database model as well, because you need a discriminator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM