-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: a easy question, why this cascade failed,Thx
PostPosted: Wed Oct 05, 2005 1:42 am 
Newbie

Joined: Wed Oct 05, 2005 1:31 am
Posts: 1
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>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 8:10 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
I am not sure that it is the cause of your problem but TOrderItemCollection should be an IList because NHibernate will replace it by its own implementation of Bag.

On side note, you don't need to do session.SaveOrUpdate(order) two times; order is kept in the session's cache and will automatically get updated when committing... (and I assume that you know that you should use try .. catch .. finally ...)

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.