-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-one insert problem
PostPosted: Wed Jun 08, 2005 12:16 pm 
When creating a new class I was hoping for 2 insert statements, but instead I get an Insert and an Update.
Code:
   <class name="NH_FF_TestApp.Class1, NH-FF-TestApp" table="ATable1">
      <id name="Id" >
         <generator class="assigned" />
      </id>
      <property name="Field1" />
   </class>

   <class name="NH_FF_TestApp.Class2, NH-FF-TestApp" table="ATable2">
      <id name="Id" >
         <generator class="assigned" />
      </id>
      <many-to-one name="Object1" class="NH_FF_TestApp.Class3, NH-FF-TestApp" column="Object1Id" cascade="all"/>      
      <property name="Field1" />
   </class>

   <class name="NH_FF_TestApp.Class3, NH-FF-TestApp" table="ATable3">
      <id name="Id" >
         <generator class="assigned" />
      </id>
      <property name="Field1" />
   </class>

Code:
      {
         Configuration cfg = new Configuration();
         cfg.AddAssembly("NH-FF-TestApp");

         NH.ISessionFactory factory = cfg.BuildSessionFactory();
         _session = factory.OpenSession();
         
         Class2 class2 = new Class2();
         class2.Id = "C2" + DateTime.Now.ToString().Replace(" ", "");
         class2.Field1 = "aaaaaa";

         Class3 class3 = new Class3();
         class3.Id = "C3" + DateTime.Now.ToString().Replace(" ", "");
         class3.Field1 = "class3";

         class2.Object1 = class3;

         _session.Save(class2);
         _session.Flush();               


This generates an insert statement for ATable2 and an update statement for ATable3 which fails as there is nothing to update.

Regards
Tim


Top
  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 1:39 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate doesn't know that the object is new, because you use an assigned identifier. You can either save the Class3 object manually using session.Save() or provide a <version> property which NHibernate will then use to determine whether the object is new or not.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 5:30 am 
Thanks once again :D
Tim


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.