-->
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.  [ 6 posts ] 
Author Message
 Post subject: Parent-Child Cascade Insert Exception
PostPosted: Wed Jun 27, 2007 4:01 pm 
Beginner
Beginner

Joined: Wed May 02, 2007 12:24 am
Posts: 41
Hello,

I have a parent/child relationship. And I call session.SaveOrUpdate to save parent. The parent save cascade to the child and saves the child.

Everything was working great, until I did the following:
Create a Parent
Create a child. Add the child to the parent.
SaveOrUpdate(Parent);

It throws an exception: "Cannot insert the value NULL into column 'ParentID' table Child..."

I'm using sqlServer Identity field for my ids. It is as if when the Parent gets insterted the new Id doesn't get reflected in the object. So when I'm trying to save the child it thinks the value is null.

Here is a bunch of snippets.

Class Parent
{
int _parentId;
int ParentId {get{...} set{...}}
IList<Child> _childeren;
IList<Child> Childeren {get{...} set{...}}
}

<class Name=Parent...>
<id name=ParentId>
<generator class="native"/>
</id>
<bag name="Childeren"
inverse="true"
lazy="false"
cascade="all-delete-orphan">
<key column="parentId"/>
<one-to-many class="ChidTable"/>
</bag>
</class>

Class Child
{
int _childId;
Parent _inverseParent;
}



...
Parent p new Parent();
Child c = new Child();
c._inverseParent = p;
p._childeren.Add(c);

SaveOrUpdate(p);
...

Parent is inserted, but when the c is getting inserted I get a
"Cannot insert the value NULL into column 'ParentID'..."

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 4:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The foreign key column in the child table must be nullable according to the documentation (or was it in Hibernate in Action?). When cascade-inserting, NHibernate insert the child object with a null parentID and then does a separate update to update the parentID column. I do not totally understand the rationale; perhaps someone else could shed some light on this.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 5:55 pm 
Beginner
Beginner

Joined: Wed May 02, 2007 12:24 am
Posts: 41
I'm sorry but what you're describing is circumvented by using the inverse=true attribute.

All of my scenarios work fine. Except when the parent doesn't exist in the db to begin with.

In other words if I create the parent and the child in memory and then save the object graph then I get the exception.

Is this normal? Is there a way to work around it?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 12:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Hmmm... I think what you described should work. Maybe someone else can weight in on this?

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 12:44 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Perhaps you simplified for your posting, but there is no unsaved-value defined on your parent class. This would definitely affect the function of SaveOrUpdate(). Also, it might help if we see your child class mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 12:50 pm 
Beginner
Beginner

Joined: Wed May 02, 2007 12:24 am
Posts: 41
my bad...

The child.InverseParent instance was not the same as Parent instance. Same entity but not the same instance. After I fixed that, it started working fine.

Thank you for your help.


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