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: Many-To-One insert not cascaded, it throws foreign key error
PostPosted: Tue Mar 11, 2008 7:26 pm 
Newbie

Joined: Tue Mar 11, 2008 7:15 pm
Posts: 1
I have a Node and NodeType class, simple foriegn key from NodeType to Node. In C# I want to create a node, create a nodetype, set the node's NodeType property to my newly created nodetype, and save the node. I was hoping the when I save the node, NHibernate would know about the foriegn key, and insert the nodetype into the nodetype table before it inserts the node into the node table. This is not the case. I keep getting a foreign key exception. Is what I am trying to do possible? thanks

Node node = new Node();
NodeType nodeType = new NodeType();
nodeType.ID = 0;
node.NodeType = nodeType;
session.Save(node);

<class name="NodeType" table="NodeType">
<id name="ID">
<column name="ID" sql-type="Int32" not-null="true"/>
<generator class="assigned" />
</id>

<set name="Nodes" cascade="all" lazy="false">
<key column="NodeTypeID" />
<one-to-many class="Node" />
</set>
</class>

<class name="Node" table="Node">

<id name="ID">
<column name="ID" sql-type="char(32)" not-null="true"/>
<generator class="identity" />
</id>

<many-to-one name="NodeType"
class="NodeType"
column="NodeTypeID"
cascade="all"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 2:59 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You also have to add the node to nodes collection in nodeType. And you should mark the set as inverse.

_________________
--Wolfgang


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.