所以我有一个名为 order 的父对象和一个名为 orderLineItems 的子对象集合,其中 Cascade 在 nHibernate 中设置为 All-Delete-orphan。这是我的代码。
using( var session = _sessionManager.GetSession())
using (var transaction = session.BeginTransaction())
{
var order = _repository.GetOrderEagerlyByOrderId(session, fromDb.Id);
var now = DateTime.Now;
const string user = "GNB\\Username";
var future = now.AddYears(1);
var taxType = new TaxType(0, "Code", "AlternateNameE", "AlternateNameF", "NameE", "NameF", "DescriptionE", "DescriptionF", 13, now, future, user, now, user, now);
var _serviceCatRep = new ServiceCatalogueRepository();
var serviceCatalogueItem = _serviceCatRep.GetServiceCatalogueItemByCode(session, "VR-PASS");
var orderLineItem1 = new OrderLineItem(0, null, "DescriptionE", "DescriptionF", 1, 10, null, null, 5, false, serviceCatalogueItem, null, user, now, user, future);
order.OrderLineItems.Clear();
order.OrderLineItems = order.OrderLineItems == null ? new List<IOrderLineItem> { orderLineItem1 } : new List<IOrderLineItem>(order.OrderLineItems) { orderLineItem1 };
_repository.SaveOrUpdate(session, order);
transaction.Commit();
}
它失败transaction.Commit();
并出现错误:
NHibernate.HibernateException:拥有的实体实例不再引用具有 cascade="all-delete-orphan" 的集合
另外,举个例子,all 和 all-delete-orphan 有什么区别。