nhibernate映射,不带主键和外键的联接 [英] nhibernate mapping, join without primary and foreign keys

查看:264
本文介绍了nhibernate映射,不带主键和外键的联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些遗留表,我可以通过nhibernate访问,基本的一个实体访问是可以的,但是我真的需要使连接正常工作.

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.

理想情况下,我将具有主键和外键来定义联接,但是由于这些是遗留表,因此我只有作为表索引的复合ID,这些索引出于性能原因而使用,因此我无法更改.

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.

无论如何我都有JobHeader表和Property表

Anyways i have JobHeader table and Property table

JobHeader映射目前看起来像这样:

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>

属性映射如下:


<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中,它使用"PropRef"保存属性"Reference".

In Jobheader it uses "PropRef" to hold the Property "Reference".

我想创建一个新的映射文件,称为JobHeaderJoinedProperty 因此可能看起来像这样:

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>

然后希望我的JobHeaderJoinedProperty实体能够访问其中的Property实体:

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

通过nhibernate连接两个旧表应该不是很棘手吧!!

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

我真的只想复制一个内部联接,其中sql将是这样的:

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

推荐答案

您所追求的不是一对一的映射吗?

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

所以您的情况应该是

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

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

这篇关于nhibernate映射,不带主键和外键的联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆