0

[マッピングファイルで更新]

今日、遅延ロード/プロキシされたオブジェクトが永続化されるという問題が発生しました。

Invoice と Address の 2 つのクラスに関連しています。請求書には Address プロパティがあります。どちらのクラスも遅延ロードされるように設定されており、すべてのメソッドは仮想です。

コードで Invoice.address = HomeCompany.address を実行すると、実行時に Invoice.address が正しく設定されていることを確認できます (Invoice.address プロパティには「アドレス プロキシ」が正しく割り当てられています)。ただし、Invoice オブジェクトが永続化されると、invoice テーブルの「addresss_id」列が「0」に設定されます。ただし、「Lazy = False」を追加してアドレスのマッピング ファイルを変更すると、すべて正常に機能します (invoice.address プロパティは完全にインスタンス化されたアドレスに設定されます)。

nHibernate 2.1.2 を使用していて、これが私を夢中にさせています。

[注: nhibernate はエラーを生成していません]

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping default-access="field.camelcase-underscore" xmlns="urn:nhibernate-mapping-2.2">
<class name="TMS.Business.invoice, TMS.Business"  dynamic-update="true" optimistic-lock="all" table="invoice">

    <id name="document_id" type="Int32">
        <generator class="assigned" />
    </id>

    <property name="create_date" />
    <many-to-one name="last_updt_user" column="last_updt_userid"/>
    <property name="last_updt_datetime" />

    <property name="amount" />
    <property name="approved_flag"/>
    <property name="ba_document_date" />
    <property name="ba_document_no" not-null="true"/>
    <property name="comment" not-null="true"/>
    <property name="document_no" />
    <property name="document_no_construct"/>
    <property name="dry_gas_billing_date" />
    <property name="due_date" />
    <property name="fin_batch_no" />
    <property name="fin_interface_type_cd"/>
    <property name="fin_process_datetime" />        
    <property name="invoice_date" />
    <property name="netout_flag"/>
    <property name="override_amount" />
    <property name="receipt_date" />
    <property name="void_flag"/>

    <many-to-one name="accountant_user" column="accountant_userid"/> 
    <many-to-one name="ba" column="ba_id" property-ref="_ba_id" /> 
    <many-to-one name="ba_addr" column="ba_addr_id" property-ref="_ba_address_id" /> 
    <many-to-one name="ba_contact" column="ba_contact_id" property-ref="_ba_contact_id" /> 
    <many-to-one name="dry_gas_billing_type" column="dry_gas_billing_type_cd" property-ref="_code_key" />
    <many-to-one name="internal_ba" column="internal_ba_id" property-ref="_ba_id" />
    <many-to-one name="invoice_subtype" column="invoice_subtype_cd" property-ref="_code_key" />
    <many-to-one name="invoice_type" column="invoice_type_cd" property-ref="_code_key" />
    <many-to-one name="payment_method" column="payment_method_cd" property-ref="_code_key" />
    <many-to-one name="payment_term" column="payment_term_id" />
    <many-to-one name="remit_to_addr" column="remit_to_addr_id" property-ref="_ba_address_id"/>

    <bag name="document_histories" lazy="true" cascade="none" inverse="true" where ="linked_table = 'invoice'" order-by="document_history_id DESC">
        <key column="linked_pk"/>
        <one-to-many class="TMS.Business.document_history, TMS.Business"/>
    </bag>
    <bag name="trxn_pricelines" lazy="true" cascade="none" inverse="true">
        <key column="document_id"/> 
        <one-to-many class="TMS.Business.trxn_priceline, TMS.Business"/>
    </bag>      

</class>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping default-access="field.camelcase-underscore" xmlns="urn:nhibernate-mapping-2.2">
<class name="TMS.Business.ba_address, TMS.Business" optimistic-lock="version" table="_ba_address">

    <id name="item_guid" type="Guid">
        <generator class="guid" />
    </id>
    <version name="version" />

    <property name="active_flag" />
    <property name="address" />
    <property name="city" />
    <property name="county" />
    <property name="remarks" />
    <property name="zip" />

    <many-to-one name="business_associate" column="business_associate_guid" />
    <many-to-one name="country" column="country_code" />
    <many-to-one name="state" column="state_code" />

    <property name="_ba_address_id" generated="insert" update="false" insert="false"/>  

</class>

4

1 に答える 1

0

アドレスの主キー(id)を使用してInvoice.Addressをマップする場合、これは発生しません。

<class name="TMS.Business.invoice, TMS.Business"  dynamic-update="true" optimistic-lock="all" table="invoice">

    <id name="document_id" type="Int32">
        <generator class="assigned" />
    </id>
    <many-to-one name="ba_addr" column="ba_addr_id"  /> 
</class>

<class name="TMS.Business.ba_address, TMS.Business" optimistic-lock="version" table="_ba_address">
    <id name="item_guid" type="Guid">
        <generator class="guid" />
    </id>
</class>

説明されている動作は、_ba_address_idがフィールドとしてマップされているように聞こえますが、マッピングファイルはそれがプロパティであることを示しています。ただし、先頭のアンダースコアと小文字の名前は、プロパティではなくフィールドをマッピングしていることを示しています。

これが実際にフィールドである場合、これが問題の原因です。プロキシオブジェクトをロードするには、その(仮想)プロパティまたはメソッドの1つにアクセスする必要があります。

于 2010-02-24T22:40:20.457 に答える