2009-11-07 77 views
0

我已經在Invoice和InvoiceDetail之間創建瞭如下所示的映射。當試圖更新發票時,我也注意到用戶的更新聲明。爲什麼要在設置cascade =「none」時更新「用戶」?NHibernate多對一級聯

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

感謝

阿努拉格

回答

0

你們是否在用戶對象的任何變化? NHibernate中的默認行爲是在會話結束時自動刷新提交更改,因此如果對用戶對象進行了更改,這些更改將在會話結束時提交。

session.FlushMode = FlushMode.Never; 

將阻止此行爲並確保只有當實體明確更新時(無論是直接還是通過關聯)才能保存更改。