2010-02-24 104 views
0

[更新與映射文件]NHibernate的延遲加載/的代理對象 - 持久性問題

遇到了一個問題,有延遲加載/代理對象被今天依然存在。

它涉及兩個類,發票和地址。發票具有地址屬性。這兩個類都被設置爲延遲加載,並且所有方法都是虛擬的。

在代碼中我做了一個Invoice.address = HomeCompany.address,我可以在運行時驗證Invoice.address設置正確(Invoice.address屬性正確分配了一個'地址代理')。但是,當發票對象持續存在時,發票表中的'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> 

+0

發佈您的映射文件。 – LordHits 2010-02-24 21:10:18

回答

0

如果您使用映射的Invoice.Address主鍵(這將不會發生id)的地址。

<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映射爲字段,但映射文件表明它是一個屬性。然而,主要的下劃線和小寫名稱表明您正在映射字段而不是屬性。

如果這實際上是一個領域,這是你的問題的原因。爲了加載代理對象,您需要訪問其(虛擬)屬性或方法之一。

+0

我不能這樣做,'發票'類是一個遺留類,它的模式已經設置。我無法更改invoice.address以使用guid引用ba_address類。 – Andrew 2010-02-25 14:24:45

+0

而ba_address._ba_address_id絕對是屬性而不是字段? – 2010-02-25 14:44:25

+0

我有一個「_ba_address_id」屬性和一個「__ba_address_id」字段。我選擇包含額外的「_」來表示這些字段是用於傳統對象的「傳統」訪問。 – Andrew 2010-02-25 16:42:55

0

難道這是級聯設置?你有沒有設置地址級聯?

+0

Cacade是'收藏'這是一個'多對一'的關係。 (發票有一個或零個地址條目) – Andrew 2010-02-25 14:28:38