Nhibernate mapping, union without primary and foreign keys

I have these legacy tables that I access on nhibernate, access to the underlying single object is ok, but I really need to work with connections.

Ideally, I would have a primary and foreign key for defining joins, but since these are obsolete tables, I only have composite identifiers that are indexes for tables, they were used indexes for performance reasons, so I cannot change.

Anyway, I have a JobHeader table and a property table

The JobHeader mapping looks something like this:


<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 the property mapping is as follows:


<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>

Jobheader uses PropRef to store the Link property.

, JobHeaderJoinedProperty , , :


<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>

, , JobHeaderJoinedProperty Property :


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

nhibernate ??

, sql :


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

, ?

<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)
/>

,

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

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

0

Source: https://habr.com/ru/post/1732528/


All Articles