【问题标题】:NHibernate Many-to-Once CascadeNHibernate 多对一次级联
【发布时间】:2009-11-07 09:18:21
【问题描述】:

我已经在 Invoice 和 InvoiceDetail 之间创建了如下所示的映射。尝试更新发票时,我也注意到用户的更新语句。当我们设置了 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>

谢谢

阿努拉格

【问题讨论】:

    标签: nhibernate many-to-one


    【解决方案1】:

    您是否对用户对象进行了任何更改? NHibernate 中的默认行为是在会话结束时自动刷新并提交更改,因此如果您对用户对象进行了更改,这些更改将在会话结束时提交。

    session.FlushMode = FlushMode.Never;
    

    将阻止这种行为,并确保仅在直接或通过关联显式更新实体时保存更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多