开发者

Mapping a non existing column in Nhibernate

开发者 https://www.devze.com 2023-02-15 06:30 出处:网络
I am working with an Oracle legacy database and I am trying to map a relationship with two tables.Unfortunately the company who designed the database used composite keys.

I am working with an Oracle legacy database and I am trying to map a relationship with two tables. Unfortunately the company who designed the database used composite keys.

I've got this table (ORDERS) which has a primary key composed on 3 fields: ordernumbe开发者_JAVA百科r (number), version and company.

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BpSalesOrders.Domain"
                   namespace="BpSalesOrders.Domain">
  <class name="Order" table="OCSAORH" mutable="false" where="OCHAMND = 0">
    <cache usage="read-only"/>
    <composite-id>
      <key-property name="Number" column="OCHORDN" type="String" length="10"></key-property>
      <key-property name="Ver" column="OCHAMND" type="Int32"></key-property>
      <key-property name="Company" column="OCHCOSC" type="String" length="5"></key-property>
    </composite-id>
    <property name="Reference" column="OCHOCNO" type="String" length="25"></property>
    <property name="Date" column="OCHOCDT" type="Int32"></property>
    <property name="Status" column="OCHSTA1" type="Char"></property>
    <property name="LineOfCreditStatus" column="OCHSTA4" type="Char"></property>
    <property name="WareHouse" column="OCHRESP" type="String"></property>
    <set name="OrderLines" access="field.pascalcase-underscore" inverse="true" lazy="true" mutable="false">
      <key>
        <column name="OCLORDN" not-null="true"/>
        <column name="OCLAMND" not-null="true" default="0"/>
        <column name="OCLCOSC" not-null="true"/>
      </key>
      <one-to-many class="OrderLine" not-found ="ignore"/>
    </set>
    <set name="OrderReleases" access="field.pascalcase-underscore" inverse="true" lazy="true" mutable="false">
      <key>
        <column name="ORLORDINE" not-null="true" />
        <column name="ORLSOCIETA" not-null="true"/>
      </key>
      <one-to-many class="OrderRelease" not-found ="ignore"/>
    </set>
  </class>
</hibernate-mapping>

There's a relation with OrderLines which works fine cause that table has the same exact key structure. I am having problem with the OrderReleases relation. That table has got a primary key composed on a OrderNumber, Company and date. There's no version here:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                     assembly="BpSalesOrders.Domain"
                     namespace="BpSalesOrders.Domain">
  <class name="OrderRelease" table="OCBPORDRIL" mutable="true">
    <composite-id>
      <key-property name="Company" column="ORLSOCIETA" type ="String" length="5"></key-property>
      <key-property name="OrderNumber" column="ORLORDINE" type ="String" length="10"></key-property>
      <key-property name="RequestDate" column="ORLDATARICHIESTA" type ="Int32"></key-property>
    </composite-id>
    <property name="Status" column="ORLSTATUS" type="Char" length="1"></property>
    <property name="StatusMessage" column="ORLSEGNALAZIONE" type="String" length="90"></property>
    <property name="ProcessingDate" column="ORLDATAELABORA" type ="Int32"></property>
  </class>
</hibernate-mapping>

Considering that the relationship is one-to-many I would like to join the table Orders and OrderRelease on OrderNumber and Company. If I try to do that (as in the example) nHibernate throws an exception telling me that the key it's trying to map is composed with 3 fields.

Is there a way to create an non-existing column which wouldn't be persisted so that I can setup my mapping and relations?

Any help will be apreciated.


Ended up creating a view with a fake column.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号