<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Emad Alashi&#039;s Blog &#187; cascade</title>
	<atom:link href="http://www.emadashi.com/index.php/tag/cascade/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emadashi.com</link>
	<description></description>
	<lastBuildDate>Sun, 15 Jan 2012 10:05:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NHibernate Inverse attribute</title>
		<link>http://www.emadashi.com/index.php/2008/08/nhibernate-inverse-attribute/</link>
		<comments>http://www.emadashi.com/index.php/2008/08/nhibernate-inverse-attribute/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 14:22:26 +0000</pubDate>
		<dc:creator>Emad Alashi</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[cascade]]></category>
		<category><![CDATA[Inverse]]></category>

		<guid isPermaLink="false">http://eashi.wordpress.com/2008/08/22/nhibernate-inverse-attribute/</guid>
		<description><![CDATA[I had to read documentation, articles, many blog entries and go into discussions with colleagues&#8230; all to get the root of this ambiguous attribute of NHibernate, it is trickey!
This blog entry is another trial to explain the attribute, but with taking one more detailed step into the explanation.
NHibernate is meant to persist objects as well [...]]]></description>
			<content:encoded><![CDATA[<p>I had to read documentation, articles, many blog entries and go into discussions with colleagues&#8230; all to get the root of this ambiguous attribute of NHibernate, it is trickey!<br />
This blog entry is another trial to explain the attribute, but with taking one more detailed step into the explanation.</p>
<p>NHibernate is meant to persist objects as well as to manage their relations to each other, lets take a look at the following example of Parent and Child classes:</p>
<div style="font-size:12pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Parent</span></p>
<p style="margin:0;">    {</p>
<p style="margin:0;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> <span style="color:#0000ff;">int</span> Id { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</p>
<p style="margin:0;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> <span style="color:#0000ff;">string</span> Name { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</p>
<p style="margin:0;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> <span style="color:#2b91af;">IList</span>&lt;<span style="color:#2b91af;">Child</span>&gt; MyChildren { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</p>
<p style="margin:0;">    }</p>
<p style="margin:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Child</span></p>
<p style="margin:0;">    {</p>
<p style="margin:0;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> <span style="color:#0000ff;">int</span> Id { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</p>
<p style="margin:0;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> <span style="color:#0000ff;">string</span> Name { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</p>
<p style="margin:0;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">virtual</span> <span style="color:#2b91af;">Parent</span> MyParent { <span style="color:#0000ff;">get</span>; <span style="color:#0000ff;">set</span>; }</p>
</div>
<p>and we have corresponding tables to these two classes like the following:</p>
<p><a href="http://eashi.files.wordpress.com/2008/08/parent.jpg"><img style="border-width:0;" src="http://eashi.files.wordpress.com/2008/08/parent-thumb.jpg" border="0" alt="Parent" width="223" height="78" /></a></p>
<p><a href="http://eashi.files.wordpress.com/2008/08/child.jpg"><img style="border-width:0;" src="http://eashi.files.wordpress.com/2008/08/child-thumb.jpg" border="0" alt="Child" width="222" height="101" /></a></p>
<p>(note 1: as we are proceeding that there is no Null constraint on the ParentId foreign key in the Child Table)<br />
Finally, we check the interesting part of the mapping files:</p>
<ol>
<li>Parent:
<div style="font-size:12pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">bag</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">MyChildren</span>&#8220;<span style="color:#0000ff;"> </span><span style="color:#ff0000;">table</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">Child</span>&#8220;<span style="color:#0000ff;"> </span><span style="color:#ff0000;">cascade</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">all</span>&#8220;<span style="color:#0000ff;">&gt;</span></p>
<p style="margin:0;"><span style="color:#0000ff;">      &lt;</span><span style="color:#a31515;">key</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">column</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">ParentId</span>&#8220;<span style="color:#0000ff;">/&gt;</span></p>
<p style="margin:0;"><span style="color:#0000ff;">      &lt;</span><span style="color:#a31515;">one-to-many</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">class</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">Child</span>&#8220;<span style="color:#0000ff;">/&gt;</span></p>
</div>
</li>
<li>Child:
<div style="font-size:12pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">many-to-one</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">MyParent</span>&#8220;<span style="color:#0000ff;"> </span><span style="color:#ff0000;">class</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">Parent</span>&#8220;<span style="color:#0000ff;"> &gt;</span></p>
<p style="margin:0;"><span style="color:#0000ff;">      &lt;</span><span style="color:#a31515;">column</span><span style="color:#0000ff;"> </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">=</span>&#8220;<span style="color:#0000ff;">ParentId</span>&#8220;<span style="color:#0000ff;">/&gt;</span></p>
<p style="margin:0;"><span style="color:#0000ff;">    &lt;/</span><span style="color:#a31515;">many-to-one</span><span style="color:#0000ff;">&gt;</span></p>
</div>
</li>
</ol>
<p>(note 2: all what the <span style="text-decoration:underline;">cascade=&#8221;all&#8221;</span> attribute in the MyChildren bag does is that when ever you save the Parent, all the Child objects in the MyChildren collection are forced to be saved as well; and we are only talking about the Save operation; not interfering or changing any of the values of the Child properties)</p>
<p>Now if we execute the following code:</p>
<div style="font-size:12pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:#2b91af;">Parent</span> par = Session.Get&lt;<span style="color:#2b91af;">Parent</span>&gt;(8);</p>
<p style="margin:0;">            <span style="color:#2b91af;">Child</span> ch = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Child</span>();</p>
<p style="margin:0;">            ch.Name = <span style="color:#a31515;">&#8220;Emad&#8221;</span>;</p>
<p style="margin:0;">            par.MyChildren.Add(ch);</p>
<p style="margin:0;">            Session.Save(par);</p>
</div>
<p>As you may expect, both objects &#8220;par&#8221; and &#8220;ch&#8221; will be saved due to note 2 we mentioned above, but the surprise is that when you check the values in the database, you will see that the ParentId field was set as well although we didn&#8217;t set it explecitly in the code!</p>
<p><a href="http://eashi.files.wordpress.com/2008/08/dbvalues.jpg"><img style="border-width:0;" src="http://eashi.files.wordpress.com/2008/08/dbvalues-thumb.jpg" border="0" alt="dbValues" width="244" height="43" /></a></p>
<p>The reason is that there is a hidden attribute called &#8220;Inverse&#8221; in the bag part of the Parent map file whose default value is &#8220;false&#8221;; when this attribute is set to false like the default value, then the Parent says: &#8220;Oh, so it is my responsibility to maintain the relationship with my child objects, ok then when ever a child is added to my collection, when I am saved&#8230;I will perform an update SQL statement to their foreign key to point at me&#8221;</p>
<p>So when the Save is called, the &#8220;par&#8221; object is saved, and the &#8220;ch&#8221; is inserted because calling Session.Save(object) when the object is new it will be inserted.  And after all that happens, an explicit update SQL statement will be executed to update all the child objects to set the foreign key ParentId to the par object Id.</p>
<p>This goes all fine in our case, only due to note 1 (scroll up again); we don&#8217;t have an Null constraint on the foreign key ParentId, so the Insert statement is excuted without exceptions, but in most cases in the world, DBA do put this constraint, by that we will get a &#8220;cannot insert Null value in ParentId&#8221; exception!</p>
<p>The solution is to set the Inverse attribute to &#8220;true&#8221;, which means that the Parent will NOT updated the Child objects foreign key, it will only call Session.Save(ch) due to the <span style="text-decoration:underline;">cascade</span> attribute, so the result will be like the record 6:</p>
<p><a href="http://eashi.files.wordpress.com/2008/08/dbvalues2.jpg"><img style="border-width:0;" src="http://eashi.files.wordpress.com/2008/08/dbvalues2-thumb.jpg" border="0" alt="dbValues2" width="244" height="62" /></a></p>
<p>But then how to solve this problem?! we want to set the value of ParentId AND be able to preserve the Null constraint; so we need to set the MyParent property of the Child to the Parent &#8220;par&#8221; like line 4:</p>
<div style="font-size:12pt;background:#f2ebe3;color:black;font-family:courier new;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:#2b91af;">Parent</span> par = Session.Get&lt;<span style="color:#2b91af;">Parent</span>&gt;(8);</p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span>             <span style="color:#2b91af;">Child</span> ch = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Child</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span>             ch.Name = <span style="color:#a31515;">&#8220;Emad&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span>             ch.MyParent = par;</p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span>             par.MyChildren.Add(ch);</p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span>             Session.Save(par);</p>
</div>
<p>and to make it graceful, we can create custom collection for the Children and in the Add method of the collection we set the passed Child objects property MyParent to the parent.</p>
<p>You can download the code sample <a href="http://www.freedrive.com/file/461790,nhibernate_inverseatt.zip">here</a>.</p>
<p>I hope this is detailed enough to explain what the Inverse attribute exactly is, how to go about the Null exception, and to understand that the attributes &#8220;Inverse&#8221; and &#8220;cascade&#8221; are different things.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadashi.com/index.php/2008/08/nhibernate-inverse-attribute/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

