nHibernate延迟加载/代理对象 - 持久性问题

时间:2010-02-24 21:03:31

标签: nhibernate persistence lazy-loading proxy-classes

[使用映射文件更新]

今天持续存在延迟加载/代理对象的问题。

它涉及两个类,Invoice和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>

2 个答案:

答案 0 :(得分: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 :(得分:0)

这可能是级联设置吗?您是否设置了要级联的地址?