-->
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: Save child along with parent
PostPosted: Sun Dec 02, 2007 9:03 am 
Beginner
Beginner

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

I have the following situation:
two tables, Child and Parent;

Child has PK column ChildId and FK column to Parent ParentId;
Parent has PK column ParentId.

.NET class Child has property Parent.

Sample code looks like this:

Configuration cfg = new Configuration();
cfg.AddAssembly( "SaveWithParent" );

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

try
{
// initialize tran
transaction = session.BeginTransaction();

// create new child
Child child = new Child();
child.Name = "Jonas Mazasis";
child.Parent = new Parent();
child.Parent.Name = "Jonas Didysis";

// tell NHibernate that this object should be saved
session.Save( child.Parent );
session.Save( child );

// 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();
}

When I execute this code, I get the following insert statments:
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 = 'Jonas Didysis', :p1 = '2'
NHibernate: INSERT INTO TEST_CHILD (PARENTID, NAME, PARENTID, CHILDID) VALUES (:p0, :p1, :p2, :p3); :p0 = '0', :p1 = 'Jonas Mazasis', :p2 = '2', :p3 = '4'

Parent is created with a first insert statement, then Child; notice, that Child creation statement has two PARENTID columns in it - first value is unspecified and second value is taken from newly inserted Parent. What I am doing wrong?

This code works when I set insert="False" in Child mapping file for property ParentId -- then first PARENTID value is removed from Child creation statement. But is this OK?


Regards,
Mindaugas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 4:07 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Mappings and classes, please.


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.