3
votes

I have these legacy tables which i’m accessing by nhibernate, basic one entity access is fine but i would really need to get joins working.

Ideally i would have primary and foreign key to define the joins but as these are legacy tables i only have composite ids that are the indexes for the tables, indexes these have been used for performance reason so i cannot change.

Anyways i have JobHeader table and Property table

JobHeader mapping looks something like this at the moment:


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="JobHeader " dynamic-update="true" table="JOB_HEADER">
    <composite-id>
      <key-property name="Company" column="JBH_COMPANY" type="String(6)" />
      <key-property name="ContractRef" column="JBH_CONTRACT_REF" type="String(10)" />
      <key-property name="JobRef" column="JBH_JOB_REF" type="String(10)" />
      <key-property name="Status" column="JBH_STATUS" type="String(10)" />
    </composite-id>
    <property name="RowId" column="TK_ROWID" type="Int32" not-null="true" />
    <property name="PropRef" column="JBH_PROP_REF" type="String(20)" not-null="false" />
  </class>
</hibernate-mapping>

And Property mapping looks like this:


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Property" dynamic-update="true" table="PROPERTY">
    <composite-id>
      <key-property name="Company" column="PRP_COMPANY" type="String(6)" />
      <key-property name="Reference" column="PRP_REFERENCE" type="String(20)" />
    </composite-id>
    <property name="RowId" column="TK_ROWID" type="Int32" not-null="true" />
    <property name="Name" column="PRP_NAME" type="String(40)" not-null="false" />
  </class>
</hibernate-mapping>

In Jobheader it uses “PropRef” to hold the Property “Reference”.

I would like to create a new mapping file that would be called JobHeaderJoinedProperty And so would perhaps look something like this:


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="JobHeaderJoinProperty" dynamic-update="true" table="JOB_HEADER">
    <composite-id>
      <key-property name="Company" column="JBH_COMPANY" type="String(6)" />
      <key-property name="ContractRef" column="JBH_CONTRACT_REF" type="String(10)" />
      <key-property name="JobRef" column="JBH_JOB_REF" type="String(10)" />
      <key-property name="Status" column="JBH_STATUS" type="String(10)" />
    </composite-id>
    <property name="RowId" column="TK_ROWID" type="Int32" not-null="true" />
    <property name="PropRef" column="JBH_PROP_REF" type="String(20)" not-null="false" />  </class>
    <bag name="Property" fetch="join" >
      <key column="Reference" property-ref="PropRef" />
      <one-to-one class="Property"/>
    </bag>
  </class>
</hibernate-mapping>

Then hoping my JobHeaderJoinedProperty entity would then be able to access the Property entity with this in it:


public virtual Property Property
        {
          get
          {
            return this.property;
          }
          set
          {
            this.property = value;
          }
        }

Joining two legacy tables via nhibernate shouldn’t be too tricky right?!

I really just want to replicate an inner join where the sql would be like this:


Select * from job_header inner join property on property.reference = job_header.propref
1

1 Answers

0
votes

Isn't a one-to-one mapping all you are after?

<one-to-one
        name="PropertyName"                                (1)
        class="Property"                                  (2)
        cascade="all|none|save-update|delete"              (3)
        constrained="true|false"                           (4)
        fetch="join|select"                                (5)
        property-ref="PropertyNameFromAssociatedClass"     (6)
        access="field|property|nosetter|ClassName"         (7)
/>

So in your case it would be

<one-to-one
        name="Property"                              
        class="ClassName"                                 
        property-ref="PropRef"  
/>

Source http://www.nhforge.org/doc/nh/en/#mapping-declaration-onetoone