-->
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: Cannot save parent with children
PostPosted: Mon Dec 03, 2007 6:07 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
Hello once again,

I have problems saving Parent along with Children.

* Parent .NET class:
Code:
int identity;
string name;
ISet<Child> children;


* Child .NET class:
Code:
int identity;
int parentId;
string name;


* Parent mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SaveWithParent" assembly="SaveWithParent">

   <class name="Parent" table="TEST_PARENT" dynamic-update="true" optimistic-lock="version">
      <id name="Identity" column="PARENTID" type="Int32" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">SEQ__TEST_PARENT</param>
         </generator>
      </id>
      <property name="Name" column="NAME" type="String" not-null="true"/>

      <set name="Children" cascade="all">
         <key column="PARENTID"/>
         <one-to-many class="Child"/>
      </set>
   </class>

</hibernate-mapping>


* Child mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SaveWithParent" assembly="SaveWithParent">

   <class name="Child" table="TEST_CHILD" dynamic-update="true">
      <id name="Identity" column="CHILDID" type="Int32" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">SEQ__TEST_CHILD</param>
         </generator>
      </id>
      <property name="ParentId" column="PARENTID" type="Int32" not-null="true"/>
      <property name="Name" column="NAME" type="String" not-null="true"/>
      
   </class>

</hibernate-mapping>


* Program .NET code:
Code:
Configuration cfg = new Configuration();
         cfg.AddAssembly( "SaveWithParent" );

         ISessionFactory factory = cfg.BuildSessionFactory();
         ISession session = factory.OpenSession();
         ITransaction transaction = null;

         try
         {
            transaction = session.BeginTransaction();

            // create and save child
            Child child = new Child();
            child.Name = "Slim Shary Jr. #1";

            // create and save parent
            Parent parent = new Parent();
            parent.Name = "Slim Shady";

            parent.Children = new HashedSet<Child>();
            parent.Children.Add( child );

            session.Save( parent );

            // commit all of the changes to the DB and close the ISession
            transaction.Commit();
         }
         catch ( System.Exception exc )
         {
            if ( transaction != null )
            {
               transaction.Rollback();
            }
         }
         finally
         {
            session.Close();
         }


* Database (Oracle 10g Release 2) statements:
Code:
NHibernate: select SEQ__TEST_PARENT.nextval from dual
NHibernate: select SEQ__TEST_CHILD.nextval from dual
NHibernate: INSERT INTO TEST_PARENT (NAME, PARENTID) VALUES (:p0, :p1); :p0 = 'Slim Shady', :p1 = '44'
NHibernate: INSERT INTO TEST_CHILD (PARENTID, NAME, CHILDID) VALUES (:p0, :p1, :p2); :p0 = '0', :p1 = 'Slim Shary Jr. #1', :p2 = '45'


Why parent id is not set for child creation statement?

Regards,
Mindaugas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 6:51 am 
Newbie

Joined: Fri Nov 30, 2007 7:44 am
Posts: 7
Try two things
1) In your Parent mapping set inverse="true" // in the line where cascade ist set
2) In your code do child.Parent = parent;

_________________
Kind regards
Uwe Lesta


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 9:12 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 1:25 pm
Posts: 24
It worked when I set
Code:
child.Parent = parent.

in the code
and
Code:
inverse="true"

in the mapping file.

Appreciate your help!


Top
 Profile  
 
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.