views:

72

answers:

1

Hi,

I have created the mapping shown below between Invoice and InvoiceDetail. When trying to update an Invoice, I notice an update statement for User as well. Why should “User” be updated when we have set cascade ="none"?

  <class name="Invoice" table="invoice" lazy="false">
    <id name="InvoiceID" column="InvoiceID" type="int">
      <generator class="native"/>
    </id>
    <property name="InvoiceDate" column="InvoiceDate" type="DateTime"></property>
    <property name="InvoiceNo" column="InvoiceNo" type="String"></property>
    <property name="TotalAmount" column="TotalAmount" type="Decimal"></property>
    <many-to-one class=User" name="User" column="UserID" cascade ="none" />
    <component name="InvoiceDetailList">
      <bag name="items" table="invoicedetail" lazy="false" cascade="all-delete-orphan" access="field" inverse="true" >
        <key column="InvoiceID" on-delete="cascade" />
        <one-to-many class="InvoiceDetail"/>
      </bag>
    </component>    
  </class>

  <class name="InvoiceDetail" table="invoicedetail" lazy="false">
    <id name="InvoiceDetailID" column="InvoiceDetailID" type="int">
      <generator class="native"/>
    </id>    
    <property name="ProductID" column="EntityID" type="int"></property>
    <property name="Quantity" column="Quantity" type="int"></property>
    <property name="TotalPrice" column="TotalPrice" type="Decimal"></property>
    <many-to-one class="Invoice" name="Invoice" column="InvoiceID" not-null="true"/>
  </class>

Thanks

Anurag

A: 

Are you making ANY changes to the user object? Default behaviour in NHibernate is to Automatically flush & commit changes at the end of session, so if you have made changes to the user object those changes will be committed at the end of the session.

session.FlushMode = FlushMode.Never;

will prevent this behaviour and ensure that changes are only saved when the entity is explicitly updated, either directly or via association.

reach4thelasers