我试图用一组子对象来持久化对象。由于存在FK关系,我不能先坚持孩子。我可以先保存父级,然后将子级添加到它上面,但这会引入更多工作。基本上我只是试图一步完成保存完全填充的对象而不是将其分成几部分。我的映射有什么问题(对不起它看起来很难看)或者是我的方法吗?
父:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NetworkOrderManagement.Core.Order, NetworkOrderManagement.Core" table="NETORDMGMT.ORDERHEADER" lazy="false" >
<id name="OrderId" column="ORDERID" type="int">
<generator class="seqhilo">
<param name="sequence">ORDERID_SEQ</param>
</generator>
</id>
<property name="TransmissionDate" column="TRANSMISSIONDATE" type="DateTime"/>
<property name="StoreNumber" column="STORENUMBER" type="Int16"/>
<property name="Department" column="DEPARTMENT" type="Int16"/>
<property name="OrderType" column="ORDERTYPE" type="Int16"/>
<property name="OrderSequence" column="ORDERSEQUENCE" type="Int16"/>
<property name="ExtractTime" column="EXTRACTTIME" type="DateTime"/>
<property name="Status" column="STATUS" type="Int16"/>
<property name="ReceivedTime" column="RECEIVEDTIME" type="DateTime"/>
<bag name="OrderDetail" table="NETORDMGMT.ORDERDETAIL"
lazy="false" cascade="all" inverse="true">
<key column="ORDERID" on-delete="cascade"/>
<one-to-many class="NetworkOrderManagement.Core.OrderDetail, NetworkOrderManagement.Core" />
</bag>
</class>
</hibernate-mapping>
子:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NetworkOrderManagement.Core.OrderDetail, NetworkOrderManagement.Core" table="NETORDMGMT.ORDERDETAIL" lazy="false">
<id name="OrderDetailId" column="ORDERDETAILID" type="int">
<generator class="seqhilo">
<param name="sequence">"ORDERDTLID_SEQ"</param>
</generator>
</id>
<many-to-one name="Order" class="NetworkOrderManagement.Core.Order, NetworkOrderManagement.Core"
column="OrderId" lazy="false" not-null="true" />
<property name="ItemNumber" column="ITEMNUMBER" type="Int32"/>
<property name="OrderQuantity" column="ORDERQUANTITY" type="Int32"/>
<property name="ErrorCode" column="ERRORCODE" type="Int32"/>
</class>
</hibernate-mapping>
这是我的例外:
Test method NetworkOrderManagement.Tests.DataAccess.QuickTests.QuickTest threw exception:
Distribution.Exceptions.DataAccessException: NHibernate Exception --->
NHibernate.PropertyValueException: not-null property references a null or transient valueNetworkOrderManagement.Core.OrderDetail.Order.
当下面的测试尝试在订单仍然是暂时的时候添加orderdetail时,我得到了这个:
[TestMethod]
public void QuickTest()
{
myOrderRepository = NetworkOrderManagement.Data.RepositoryFactory.Instance.GetOrderRepository();
myOrderDetailRepository = NetworkOrderManagement.Data.RepositoryFactory.Instance.GetOrderDetailRepository();
myOrder = new Order { StoreNumber = RandGen.LittleRand(), Department = RandGen.LittleRand(), TransmissionDate = DateTime.MinValue, ExtractTime = DateTime.MinValue, ReceivedTime = DateTime.MinValue };
myOrder = myOrderRepository.Save(myOrder);
myOrderDetail1 = new OrderDetail {OrderId = myOrder.OrderId, ItemNumber = RandGen.BigRand(), OrderQuantity = RandGen.LittleRand() };
myOrderDetail2 = new OrderDetail {OrderId = myOrder.OrderId, ItemNumber = RandGen.BigRand(), OrderQuantity = RandGen.LittleRand() };
myOrderDetail1 = myOrderDetailRepository.Save(myOrderDetail1);
myOrderDetail2 = myOrderDetailRepository.Save(myOrderDetail2);
myOrder.OrderDetail.Add(myOrderDetail1);
myOrder.OrderDetail.Add(myOrderDetail2);
myOrderRepository.CommitChanges();
myOrderDetailRepository.Delete(myOrderDetail2);
myOrderRepository.CommitChanges();
myOrderRepository.Delete(myOrder);
myOrderRepository.CommitChanges();
}
答案 0 :(得分:6)
指定集合上的级联,让NHibernate为您解决
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html
好的,我已经看到你已经做到了。 :) 你还没有做的是指定反向引用。 我的意思是:您在集合中添加了一个项目,但是这个添加的项目有一个属性给它的所有者,你没有设置:
Order o = new Order();
OrderDetail detail = new OrderDetail ();
detail.Order = o;
o.OrderLines.Add (detail);
甚至更好(imho)就是这个(简化):
public class Order
{
private IList<OrderDetail> _details = new List<OrderDetail>();
public ReadOnlyCollection<OrderDetail> Details
{
return new List(_details).AsReadOnly();
}
public void AddOrderLine( OrderDetail d )
{
d.Order = this;
_details.Add (d);
}
public void RemoveOrderLine( OrderDetail d )
{
_details.Remove (d);
}
}