2010-02-12 5 views
3

J'ai ces tables héritées auxquelles j'accède par nhibernate, l'accès de base à une entité est bien, mais j'aurais vraiment besoin de faire fonctionner les jointures. Idéalement, je voudrais avoir des clés primaires et étrangères pour définir les jointures, mais comme ce sont des tables héritées, je n'ai que des identifiants composites qui sont les index pour les tables, les index ont été utilisés pour des raisons de performance, donc je ne peux pas changer.nhibernate mapping, jointure sans clés primaires et étrangères

Anyways i ont la table JobHeader et table de propriété cartographie

de JobHeader ressemble à ceci au 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> 

et la cartographie de la propriété ressemble à ceci:


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

En Jobheader il utilise "PropRef" pour contenir la propriété "Reference".

Je voudrais créer un nouveau fichier de mappage qui serait appelé JobHeaderJoinedProperty Et serait peut-être ressembler à ceci:


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

espérant alors mon entité JobHeaderJoinedProperty serait alors en mesure d'accéder à l'entité de la propriété avec ce en elle:


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

joindre deux tables héritées via NHibernate ne devrait pas être trop difficile à droite ?!

Je veux juste reproduire une jointure où le sql serait comme ceci:


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

Répondre

0

est-ce pas un-à-un tout ce que vous êtes après?

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

Donc, dans votre cas, il serait

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

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