<?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>Blog of developer Mikkel Ovesen &#187; orm</title>
	<atom:link href="http://blog.ovesens.net/tag/orm/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ovesens.net</link>
	<description>My thoughts, stuff I need to remember or things I just want to share with the world</description>
	<lastBuildDate>Thu, 19 Jan 2012 11:55:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate dirty problem, weird updates in NHprof</title>
		<link>http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/</link>
		<comments>http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 11:01:40 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nh3]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=352</guid>
		<description><![CDATA[Upgrading to NH3 from NH2.1.2 We have recently update to NHibernate 3 in one of our projects. While testing I was suddenly seeing some weird UPDATE statements in NHprof. It seemed like the NHibernate IsDirty detection had changed. After some &#8230; <a href="http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Upgrading to NH3 from NH2.1.2</h2>
<p>We have recently update to NHibernate 3 in one of our projects. While testing I was suddenly seeing some weird UPDATE statements in NHprof.</p>
<p>It seemed like the NHibernate IsDirty detection had changed.</p>
<p>After some digging it turned out that our mapping files (made in FluentNhibernate) actually contained some minor errors when compared to the DB schema. Minor errors like value typed properties not being nullable when the ClassMap said it should be.</p>
<h2>Debugging of NHibernate projects</h2>
<p>I found that inserting the following into the classmaps made debugging a lot easier:</p>
<pre class="brush: csharp;">DynamicUpdate();</pre>
<p>Now only the properties that are Dirty (according to NHibernate) are used in the SQL UPDATE statement. Here are the description from NHibernate docs:</p>
<ul>
<li><strong><tt>dynamic-update</tt></strong> (optional, defaults to <tt>false</tt>): Specifies that <tt>UPDATE</tt> SQL should be generated at runtime and contain only those columns whose values have changed.</li>
<li><strong><tt>dynamic-insert</tt></strong>: (optional, defaults to <tt>false</tt>): Specifies that <tt>INSERT</tt> SQL should be generated at runtime and contain only the columns whose values are not null.</li>
</ul>
<p>That led me directly to the property in the mapping files that had errors in it, and I could easily fix it.</p>
<h2>Conclusion</h2>
<p>So, in our case, it wasn&#8217;t really the NHibernate dirty detection that changed, NHibernate 3 is just not as fault tolerant, than that of NH2. So the upgrade made us discover some errors that should be corrected.</p>
<p>As such, this is a good thing, but it would have been nicer if NH2 had made us aware of these errors in the first place.</p>
<p>But hey, better late than never <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/03/nhibernate-dirty-problem-weird-updates-in-nhprof/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate CompositeId and GetHashCode SELECT N+1 problem</title>
		<link>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/</link>
		<comments>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 20:34:59 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/?p=337</guid>
		<description><![CDATA[I was looking in my NHProf log and got a lot of SELECT request. To many and I thought I was having a SELECT N+1 problem. Using the &#8220;Stack Trace&#8221; in NHprof I identified a call being made to GetHashCode() &#8230; <a href="http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was looking in my NHProf log and got a lot of SELECT request. To many and I thought I was having a SELECT N+1 problem.</p>
<p>Using the &#8220;Stack Trace&#8221; in NHprof I identified a call being made to GetHashCode() of the class (<em>MyClass</em>) holding the composite id. <em>MyClass</em> looked like this:</p>
<pre class="brush: csharp;">public class MyClass
{
    public virtual Key1Class Key1 { get; set; }
    public virtual Key2Class Key2 { get; set; }

    /*
     * Other virtual properties
     * ...
     */

    public override int GetHashCode()
    {
        unchecked
        {
            return ((Key1 != null ? Key1.GetHashCode() : 0) * 397) ^ (Key2 != null ? Key2.GetHashCode() : 0);
        }
    }
}</pre>
<p>Note that Key1 and Key2 are both references, and please take a closer look at the GetHashCode method.</p>
<h2>The problem</h2>
<p>The problem I was facing&#8230; NHibernate called the GetHashCode() of <em>MyClass</em> when doing the query. And the call to <em>MyClass</em>&#8216;s GetHashCode method subsequentily called GetHashCode() of both the Key1 and Key2 classes.</p>
<p>Key1 and Key2 were loaded as proxies and hence a call to something else than theirs Id resulted in a select being fired.</p>
<h2>The solution</h2>
<p>The solution was to rewrite the GetHashCode method to this:</p>
<pre class="brush: csharp;">public override int GetHashCode()
{
    unchecked
    {
        return ((Key1 != null ? Key1.Id : 0) * 397) ^ (Key2 != null ? Key2.Id : 0);
    }
}</pre>
<p>Note that both Id&#8217;s of KeyClass1 and KeyClass2 are integers.</p>
<p>Now a call to <em>MyClass</em> GetHashCode does not result in individual SELECT statements being fired, even though the Keys area lazy loaded.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2011/02/nhibernate-compositeid-and-gethashcode-select-n1-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Configurations for NHibernate (HybridSessionBuilder)</title>
		<link>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/</link>
		<comments>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 19:31:04 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2009/01/24/Multiple-Configurations-for-NHibernate-(HybridSessionBuilder).aspx</guid>
		<description><![CDATA[&#160; I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions. This post is also &#8230; <a href="http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>&#160;</h2>
<p>I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.</p>
<p>This post is also written because of a simple question: <em>Does the HybridSessionBuilder support multiple configurations</em>?</p>
<p>Why this question? Well, I have a project where some repositories uses one database and some other uses another database.</p>
<h2>Original implementation</h2>
<p>My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:</p>
<p><a title="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/">http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/</a></p>
<p>But as you can see:<a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="283" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb.png" width="544" border="0" /></a></p>
<p>The original implementation used a static session factory and session, so this is does not support multiple configurations.</p>
<h2>CodeCampServer/Tarantino</h2>
<p>I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.</p>
<p>But by viewing the source code for <a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a> I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Furthermore I found out that the HSB now is a part of a collection of libraries called <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino</a>.</p>
<p>I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.</p>
<h2>Test web application </h2>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="156" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_1.png" width="198" align="right" border="0" /></a> Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.</p>
<p>nhibernate.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true</em></p>
<p>nhibernate2.cfg.xml used this connection string:</p>
<p><em>Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true</em></p>
<p>My windsor.config.xml looks like this:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;firstRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;secondRepository&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span>hibernate2.cfg.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ConfigurationFile</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
</p></div>
</div>
<p><font face="Lucida Sans Unicode"></font></p>
<p>It basically just configures the two repositories with each own configuration.</p>
<p>But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_6.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_2.png" width="534" border="0" /></a> </p>
<p>Only the default NHibernate configuration file was used.</p>
<h2>Bug or feature?</h2>
<p>That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.</p>
<p>I was expecting an object structure like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_12.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="178" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_5.png" width="534" border="0" /></a> </p>
<p>But I got something like this:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_10.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="185" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_4.png" width="534" border="0" /></a>&#160; I identified the following method to be the central, when the session was instantiated:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> ISession getExistingOrNewSession(ISessionFactory factory, <span style="color: #0000ff">string</span> configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>         {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span>             <span style="color: #0000ff">if</span> (HttpContext.Current != <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   5:</span>                 var session = GetExistingWebSession();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   7:</span>                 <span style="color: #0000ff">if</span> (session == <span style="color: #0000ff">null</span> || !session.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>                 {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   9:</span>                     session = openSessionAndAddToContext(factory, configurationFile);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>                 }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  11:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>                 <span style="color: #0000ff">return</span> session;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  13:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  15:</span>             var currentSession = _currentSessions.ContainsKey(configurationFile)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>                                     ? _currentSessions[configurationFile]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  17:</span>                                     : <span style="color: #0000ff">null</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>             <span style="color: #0000ff">if</span> (currentSession == <span style="color: #0000ff">null</span> || !currentSession.IsOpen)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  19:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span>                 _currentSessions[configurationFile] = OpenSession(factory);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  21:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span>&#160; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">  23:</span>             <span style="color: #0000ff">return</span> _currentSessions[configurationFile];</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  24:</span>         }</pre>
</p></div>
</div>
<h2>Patch/solution</h2>
<p>The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.</p>
<p>I changed it to:</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> var session = GetExistingWebSession(configurationFile);</pre>
</p></div>
</div>
<p>&#160;</p>
<p>And tadaaa:</p>
<p><a href="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="42" alt="image" src="/wp-content/uploads/archive/WindowsLiveWriter/MultipleConfigurationsforNHibernateusing_D333/image_thumb_6.png" width="534" border="0" /></a> </p>
<p>The second repository now uses the second configuration file <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.</p>
<p>First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.</p>
<p>Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.</p>
<p>And BTW &#8211; I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/multiple-configurations-for-nhibernate-hybridsessionbuilder/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate &#8211; many-to-many relationship with data attached</title>
		<link>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/</link>
		<comments>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 15:09:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2009/01/07/NHibernate-many-to-many-relationship-with-data-attached.aspx</guid>
		<description><![CDATA[I just found a good blog post about the may-to-many relationship in NHibernate. The configuration sis done in Fluent NHibernate. http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx]]></description>
			<content:encoded><![CDATA[<p>
I just found a good blog post about the may-to-many relationship in NHibernate.
</p>
<p>
The configuration sis done in Fluent NHibernate.
</p>
<p>http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/01/nhibernate-many-to-many-relationship-with-data-attached/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ayende explains cascading with NHibernate</title>
		<link>http://blog.ovesens.net/2008/12/ayende-explains-cascading-with-nhibernate/</link>
		<comments>http://blog.ovesens.net/2008/12/ayende-explains-cascading-with-nhibernate/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 22:12:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2008/12/18/Ayende-explains-cascading-with-NHibernate.aspx</guid>
		<description><![CDATA[To continue my little &#34;post with good links&#34;&#8230; here is a good article by Ayende, explaining cascadin with NHibernate: http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx Here is the basics: Here is what each cascade option means: none &#8211; do not do any cascades, let the &#8230; <a href="http://blog.ovesens.net/2008/12/ayende-explains-cascading-with-nhibernate/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
To continue my little &quot;post with good links&quot;&#8230; here is a good article by Ayende, explaining cascadin with NHibernate:
</p>
<p>
<a href="http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx">http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx</a>
</p>
<p>
Here is the basics:
</p>
<p>
Here is what each cascade option means:
</p>
<ul>
<li>none &#8211; do not do any cascades, let the users handles them by themselves.</li>
<li>save-update &#8211; when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).</li>
<li>delete &#8211; when the object is deleted, delete all the objects in the assoication.</li>
<li>delete-orphans &#8211; when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.</li>
<li>all &#8211; when an object is save/update/delete, check the assoications and save/update/delete all the objects found.</li>
<li>all-delete-orhpans &#8211; when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.</li>
</ul>
<p>
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/12/ayende-explains-cascading-with-nhibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning NHibernate</title>
		<link>http://blog.ovesens.net/2008/11/learning-nhibernate/</link>
		<comments>http://blog.ovesens.net/2008/11/learning-nhibernate/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 21:30:00 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">/post/2008/11/17/Learning-NHibernate.aspx</guid>
		<description><![CDATA[NHibernate is a port of Hibernate Core for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for &#8230; <a href="http://blog.ovesens.net/2008/11/learning-nhibernate/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
NHibernate is a port of <a href="http://www.hibernate.org/344.html">Hibernate Core</a> for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in your source code.
</p>
<p>
Update 18-11-2008, well it turs out there infect is a NHibernate 2.0documentation here:
</p>
<p>
<a href="http://nhforge.org/doc/nh/en/index.html">http://nhforge.org/doc/nh/en/index.html</a>
</p>
<p>
&nbsp;
</p>
<p>
A very good place to start is here:
</p>
<p>
<a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/" title="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/">http://www.hibernate.org/hib_docs/v3/reference/en-US/html/</a>
</p>
<h2>Properties</h2>
<p>
Chapter 5 explains setting up properties in the mapping file, tags and attributes.
</p>
<p>
<a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html" title="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html">http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html</a>
</p>
<h2>Collections</h2>
<p>
Chapter 6 is about collections and lists
</p>
<p>
<a href="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html" title="http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html">http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html</a>
</p>
<h2>Many-to-many relations</h2>
<p>
Handling many-to-many relations can be difficult, especially when it comes to cascading and data integrity. This blog post explains one way of handling it.
</p>
<p>
<a href="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx" title="http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx">http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2008/11/learning-nhibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

