<?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; nh</title>
	<atom:link href="http://blog.ovesens.net/tag/nh/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>Must read and references</title>
		<link>http://blog.ovesens.net/2010/01/must-read-and-references/</link>
		<comments>http://blog.ovesens.net/2010/01/must-read-and-references/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 13:56:11 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/2010/01/must-read-and-references/</guid>
		<description><![CDATA[Read: http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/ Reference: http://west-wind.com/weblog/posts/132081.aspx]]></description>
			<content:encoded><![CDATA[<p>Read:<br />
<a href="http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/" target="_blank">http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/</a></p>
<p>Reference:<br />
<a href="http://west-wind.com/weblog/posts/132081.aspx" target="_blank">http://west-wind.com/weblog/posts/132081.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2010/01/must-read-and-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate Fluent Criteria</title>
		<link>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/</link>
		<comments>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 09:08:10 +0000</pubDate>
		<dc:creator>Mikkel Ovesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[fluent]]></category>
		<category><![CDATA[nh]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/</guid>
		<description><![CDATA[&#8211; Updated on the 2. January 2010 I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like &#8220;magic strings&#8221; as these makes the code resistent &#8230; <a href="http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>&#8211; Updated on the 2. January 2010</em></p>
<p>I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like &#8220;magic strings&#8221; as these makes the code resistent to refactoring.</p>
<p>NHibernate makes use of &#8220;magic strings&#8221; when doing a query using HQL or the Criteria API. I tend to use the Criteria API as this, opposed to HQL, limits the logic encompassed in &#8220;magic strings&#8221;.</p>
<p>As I mostly use the Criteria API, I thought, why not do something about it <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Â So I did. Very quickly a Fluent Criteria interface was implemented. Here is how it is used:</p>
<pre class="brush:csharp">var email = "email@email.com";
var customerType = CustomerType.Default;

var result = nhSession.Criteria&lt;Customer&gt;()
             .Add.Equals(x =&gt; x.Email, email)
             .Add.Equals(x =&gt; x.Type, customerType)
             .Order.Desc(x =&gt; x.Created)
             .NHCriteria
                 .SetMaxResults(1)
                 .UniqueResult&lt;Customer&gt;();</pre>
<p>In this line we call the extension method Criteria with Customer as a generic argument:</p>
<pre class="brush:csharp">var result = nhSession.Criteria&lt;Customer&gt;()</pre>
<p>By doing so we of the alternate Fluent Criteria API I have implemented. Currently it is only some of the Restrictions and Order classes that have been implemented, but you are free to use these classes and extend them as you wish.</p>
<p>By calling the NHCriteria property, you get the normal NHibernate criteria API, so here you can set the cache options, max results, etc.</p>
<p>Download the source code here <a href="http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&amp;referringTitle=Home">http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&amp;referringTitle=Home</a></p>
<p>Happy New Year btw <img src='http://blog.ovesens.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ovesens.net/2009/12/nhibernate-fluent-criteria/feed/</wfw:commentRss>
		<slash:comments>0</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>

