views:

290

answers:

2

[Updated with mapping files]

Ran into an issue with a lazy loaded / proxied object being persisted today.

It relates to two classes, Invoice and Address. A invoice has an Address property. Both classes are setup to be lazy loaded and all methods are virtual.

In the code I do a Invoice.address = HomeCompany.address and I can verify at runtime that the Invoice.address is being set correctly (The Invoice.address property is correctly assigned an 'address proxy'). However when the Invoice object gets persisted, the 'addresss_id' columns in the invoice table is being set to '0'. If however, i change the mapping file for the address by adding 'Lazy = False' everything works just fine (the invoice.address property is set to a full instantiated address).

Using nHibernate 2.1.2 and this is driving me crazy.

[Note: nhibernate is not generating any errors]

<?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>

A: 

This will not happen if you map Invoice.Address using the primary key (id) of 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>

The described behaviour sounds like _ba_address_id is mapped as a field, but your mapping file suggests it is a property. However the leading underscore and lower case names suggest you are mapping fields rather than properties.

If this is in fact a field, this is the cause of your problem. In order to load a proxy object, you need to access one of its (virtual) properties or methods.

Lachlan Roche
I cannot do this, the 'Invoice' class is a legacy class and its schema is already set. I cannot change invoice.address to use a guid to reference the ba_address class.
Andrew
And ba_address._ba_address_id is definitely a property rather than a field?
Lachlan Roche
I have a "_ba_address_id" property and a "__ba_address_id" field. I chose to include the extra "_" to denote to myself that these fields were for 'legacy' access by legacy objects.
Andrew
A: 

Could this be cascade settings? Have you set the addresses to be cascaded?

Chev
Cacade is for 'collections' this is a 'many-to-one' relationship. (An invoice has one or zero address entries)
Andrew