带有Web服务的NHibernate父/子孤立记录 [英] NHibernate Parent/Child Orphaned Records with Web Service

查看:64
本文介绍了带有Web服务的NHibernate父/子孤立记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受发票的Web服务,其中包含LineItem子级.然后,它将使用NHibernate更新数据库以创建或更新发票.

I have a web service that accepts an Invoice, which contains LineItem children. It then updates the database to either create or update the Invoice using NHibernate.

更新发票后,它将连同现在拥有的所有LineItem子项一起传递到Web服务.添加和更新工作完美.但是,如果Web Service使用者从先前保留的发票中删除了子LineItem并重新提交,则该LineItem实际上并未从数据库中删除,而是将其对父项的反向引用设置为NULL.我正在使用(尝试使用)cascade ="all-delete-orphan",但未成功.

When an invoice is updated, it is passed to the web service along with all LineItem children it now has. Adds and updates work perfectly. However, if a child LineItem is deleted from a previously persisted Invoice by the Web Service consumer and re-submitted, that LineItem is not actually removed from the database, but rather it's back reference to the parent is set to NULL. I am using (trying to use) cascade="all-delete-orphan" without success.

我怀疑问题可能是由于操作的无状态性质造成的(我首先没有在Web服务端在Invoice.LineItemList中添加LineItem,然后将其删除,而只是获得了LineItem的列表,如下所示:他们现在应该是).但是,NHibernate足够聪明,可以使后向引用列为空,因此我希望有一种直接的方法来使它删除该行.

I suspect that the problem might be due to the stateless nature of the operation (I don't first have the LineItem in Invoice.LineItemList on the web service side and then delete it, but rather just get a list of LineItem's as they now should be). However, NHibernate IS smart enough to null the back-reference column, so I hope there's a straightforward way to get it to delete that row instead.

这里是映射(简化).

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

Child Objects (LineItems):

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

<property name="LineItemNumber" />
<property name="SalesAmount"/>

Web服务持久性代码如下:

The Web Service persistence code looks like this:

[WebMethod]

公共发票PutInvoice(发票) { //必须重建父级引用,请参见博客

public Invoice PutInvoice(Invoice invoice) { // Necessary to rebuild parent references, see 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

}

推荐答案

对于对象的分离状态,这是正确的,这是对性能的一种已知限制,NHibernate将其描述为未实现"的功能.持续性".但是,您当然可以在没有有效发票参考的情况下轻松删除所有LineItem,但我也不喜欢这种解决方案. 通常,我使用客户端对象来实现无状态,这当然会导致在操作之前加载发票.

You are right this has to to with the detached state of your objects and is a known limitation in admission to performance which NHibernate describes as the not implemented feature of 'persistence of reachability'. However you could of course easily delete all LineItems without valid invoice reference but i also don't like this solution. Usually i use client objects to achieve statelessness which of course results in loading the invoice before manipulating.

这篇关于带有Web服务的NHibernate父/子孤立记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆