代码之家  ›  专栏  ›  技术社区  ›  Eric J.

使用Web服务生成父/子孤立记录

  •  3
  • Eric J.  · 技术社区  · 15 年前

    我有一个接受发票的Web服务,其中包含lineitem子项。然后它更新数据库,使用nhibernate创建或更新发票。

    当更新发票时,它将与它现在拥有的所有行项目子项一起传递给Web服务。添加和更新工作正常。但是,如果Web服务使用者从以前保留的发票中删除子行项目并重新提交,则该行项目实际上不会从数据库中删除,而是将其对父行的返回引用设置为空。我正在使用(尝试使用)cascade=“all delete orphan”但没有成功。

    我怀疑问题可能是由于操作的无状态性质造成的(我没有首先在Web服务端的invoice.lineitem list中包含lineitem,然后将其删除,而是按照现在的情况获取lineitem的列表)。但是,nhibernate足够聪明,可以让后面的引用列为空,所以我希望有一种直接的方法可以让它删除该行。

    以下是映射(简化)。

    Parent object (Invoice):
    

    <property name="InvoiceNumber" />
            <!-- If inverse="true", InvoiceId is NOT set to NULL and the record remains -->
    <bag name="LineItemList" table="lineitems" inverse="false"  cascade="all-delete-orphan">
      <key column="InvoiceId"/>
      <one-to-many 
        class="LineItem"/>
    </bag>    
    

    子对象(LineItems):

    <many-to-one lazy="false" name="Parent" column="InvoiceID" not-null="false"
                 class="Invoice,Company.Business"
                 />
    
    <property name="LineItemNumber" />
    <property name="SalesAmount"/>
    

    Web服务持久性代码如下所示:

    [WebMethod]
    

    公共发票PutInvoice(发票发票) { //需要重新生成父引用,请参见 Blog

    foreach (LineItem item in invoice.LineItems)
    {
        item.Parent = invoice;
    }
    
    using (PersistenceManager pm = new PersistenceManager())
    {
        pm.Save<Invoice>(invoice);
    }
    
    return invoice; // Return version potentially modified with DB-assigned ID
    

    }

    1 回复  |  直到 15 年前
        1
  •  1
  •   zoidbeck    15 年前

    你是对的,这与你的对象的分离状态有关,是一个已知的性能限制,NHibernate将其描述为“可到达性的持久性”的未实现特性。当然,您可以在没有有效发票参考的情况下轻松删除所有行项目,但我也不喜欢此解决方案。 通常我使用客户机对象来实现无状态性,这当然会导致在操作之前加载发票。