I want to insert 1 order object with 2 related orderItem objects
TOrderItemCollection is inherited from ArrayList and as a member of Torder class.
Here is my code. It failed for
Quote:
Lusiqing.Test.Persistance.TOrderTestFixture.TestInsertRecord : NHibernate.ADOException : Could not save object
----> NHibernate.NonUniqueObjectException : a different object with the same identifier value was already associated with the session: 872, of class: Lusiqing.Data.Entites.TOrderItem
Code:
Configuration cfg = new Configuration();
cfg.Configure();
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
TOrder order = new TOrder();
order.FId = (long)(new Random()).Next(1000);
order.FBuyerAddress = "BuyerAddress";
order.FBuyerId = "FBuyerId";
order.FBuyerMobile = "FBuyerMobile";
order.FBuyerName = "FBuyerName";
order.FBuyerPhone = "FBuyerPhone";
order.FCurrencyUnit = "FCurrencyUnit";
order.FInsertedTime = DateTime.Now;
order.FInsertedTimeUtc = DateTime.Now;
order.FLastUpdatedTime = DateTime.Now;
order.FLastUpdatedTimeUtc = DateTime.Now;
order.FPayMethod = "FPayMethod";
order.FStatus = 1;
order.TOrderItemCollection = new TOrderItemCollection();
session.SaveOrUpdate(order);
TOrderItem orderItem = new TOrderItem();
orderItem.FId = (long)(new Random()).Next(1000);
orderItem.FInsertedTime = DateTime.Now;
orderItem.FInsertedTimeUtc = DateTime.Now;
orderItem.FLastUpdatedTime = DateTime.Now;
orderItem.FLastUpdatedTimeUtc = DateTime.Now;
orderItem.FOrderId = order.FId;
orderItem.FPrice = 12.00M;
orderItem.FQuantity = 1;
orderItem.TOrder = order;
TOrderItem orderItem1 = new TOrderItem();
orderItem1.FId = (long)(new Random()).Next(1000);
orderItem1.FInsertedTime = DateTime.Now;
orderItem1.FInsertedTimeUtc = DateTime.Now;
orderItem1.FLastUpdatedTime = DateTime.Now;
orderItem1.FLastUpdatedTimeUtc = DateTime.Now;
orderItem1.FOrderId = order.FId + 1;
orderItem1.FPrice = 12.00M;
orderItem1.FQuantity = 1;
orderItem1.TOrder = order;
order.TOrderItemCollection.Add(orderItem1);
order.TOrderItemCollection.Add(orderItem);
session.SaveOrUpdate(order);
transaction.Commit();
session.Close();
TOrder.hbm.xml
Code:
<class name="Lusiqing.Data.Entites.TOrder, Lusiqing.Data.Entities" table="TOrder">
<id name="FId" column="FId" type="Int64" unsaved-value="any">
<generator class="assigned" />
</id>
<property name="FBuyerAddress" type="String"/>
<property name="FBuyerId" type="String"/>
<property name="FBuyerMobile" type="String"/>
<property name="FBuyerName" type="String"/>
<property name="FBuyerPhone" type="String"/>
<property name="FCurrencyUnit" type="String"/>
<property name="FPayMethod" type="String"/>
<property name="FStatus" type="Int32"/>
<property name="FInsertedTime" type="DateTime"/>
<property name="FLastUpdatedTime" type="DateTime"/>
<bag name="TOrderItemCollection" table="TOrderItem" inverse="true" cascade="save-update">
<key column="FOrderId" />
<one-to-many class="Lusiqing.Data.Entites.TOrderItem, Lusiqing.Data.Entities"
/>
</bag>
</class>
TOrderItem
Code:
<class name="Lusiqing.Data.Entites.TOrderItem, Lusiqing.Data.Entities" table="TOrderItem">
<id name="FId" column="FId" type="Int64" unsaved-value="any">
<generator class="assigned" />
</id>
<property name="FCDId" type="Int64"/>
<property name="FPrice" type="Decimal"/>
<property name="FQuantity" type="Int32"/>
<property name="FInsertedTime" type="DateTime"/>
<property name="FLastUpdatedTime" type="DateTime"/>
<many-to-one name="TOrder" column="FOrderId" not-null="true" />
</class>