<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://bloggingabout.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ramon Smits : NHibernate</title><link>http://bloggingabout.net/blogs/ramon/archive/tags/NHibernate/default.aspx</link><description>Tags: NHibernate</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>WCF and DateTime.Kind pitfall</title><link>http://bloggingabout.net/blogs/ramon/archive/2010/05/19/wcf-and-datetime-kind-pitfall.aspx</link><pubDate>Wed, 19 May 2010 08:48:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:483295</guid><dc:creator>Ramon Smits</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/rsscomments.aspx?PostID=483295</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/commentapi.aspx?PostID=483295</wfw:comment><comments>http://bloggingabout.net/blogs/ramon/archive/2010/05/19/wcf-and-datetime-kind-pitfall.aspx#comments</comments><description>&lt;p&gt;At my job (&lt;a href="http://www.companywebcast.com"&gt;Company Webcast&lt;/a&gt;) we have several API&amp;rsquo;s for our customers to use. One of those API&amp;rsquo;s allow webcasts to be created and modified and that interface has data containing dates. Our platform works with UTC date time&amp;rsquo;s as we are an international operating company so it is logical for use to store those as UTC.&lt;/p&gt;
&lt;p&gt;We use WCF and yesterday we had a very weird issue where calls failed. After investigating the issue we found out that the cause was that how the date time got supplied to our service in the message&lt;/p&gt;
&lt;p&gt;The following values are valid in an XML message:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;lt;ns1:ScheduledStart&amp;gt;2010-05-26T17:00:00+02:00&amp;lt;/ns1:ScheduledStart&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;ns1:ScheduledStart&amp;gt;2010-05-26T15:00:00Z&amp;lt;/ns1:ScheduledStart&amp;gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A WCF service &lt;strong&gt;accepts both values but it treats them different&lt;/strong&gt; which I did not expect! The first value became 2010-05-26 17:00 where DateTime.Kind is set to Local and the second becomes 2010-05-26 15:00 with DateTime.Kind set to UTC. &lt;strong&gt;This amazed me a bit as I assumed that both would always result in either a UTC or Local DateTime&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The reason it fails is that another argument states the time-zone from where the live webcast will be held. This is used in combination with the DateTime to convert the DateTime value to a local time to inform the viewer about the conditions of the event. This code assumed that the incoming DateTime value would always be of kind UTC.&lt;/p&gt;
&lt;p&gt;So now our front-end api&amp;rsquo;s convert incoming DateTime values to a DateTime value with kind UTC.&lt;/p&gt;
&lt;p&gt;This could also be a problem when you persist this DateTime to for example a database and your storage logic does not convert the DateTime from/to UTC or Local depending on your needs. We use NHibernate for storage and it does not by default has a way to set UTC/Local to a &amp;lt;property&amp;gt; definition. This can really become a problem when time is part of your business logic as it is in ours as we use it to schedule tasks and it is very important to know it a time is UTC or not especially when something happens on the other side of the world.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=483295" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/WCF/default.aspx">WCF</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/DateTime/default.aspx">DateTime</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/Validation/default.aspx">Validation</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/UTC/default.aspx">UTC</category></item><item><title>How to perform a not equals comparison with NHibernate criteria api</title><link>http://bloggingabout.net/blogs/ramon/archive/2010/04/20/how-to-perform-a-not-equals-comparison-with-nhibernate-criteria-api.aspx</link><pubDate>Tue, 20 Apr 2010 19:40:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:483132</guid><dc:creator>Ramon Smits</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/rsscomments.aspx?PostID=483132</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/commentapi.aspx?PostID=483132</wfw:comment><comments>http://bloggingabout.net/blogs/ramon/archive/2010/04/20/how-to-perform-a-not-equals-comparison-with-nhibernate-criteria-api.aspx#comments</comments><description>&lt;p&gt;I often hear that NHibernate is not usable for selecting records as it is not possible to perform a not equal comparison with the criteria api. I must admin that it took me a while before I found out but it really is more logical when you know. Lets take a look at the following example:&lt;/p&gt;
&lt;pre&gt;// Select entries where name = &amp;quot;Ramon&amp;quot;&lt;br /&gt;var criteria = DetachedCriteria.For&amp;lt;MyCoolClass&amp;gt;()&lt;br /&gt;.Add(Restrictions.Eq(&amp;quot;Name&amp;quot;, &amp;quot;Ramon&amp;quot;));&lt;/pre&gt;
&lt;p&gt;Your first reaction it to find the opposite of Restrictions.Eq but then you are amazed that it does not exists. The solution is so simple that it will almost embares you:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;// Select entries where name != &amp;quot;Ramon&amp;quot;&lt;br /&gt;var criteria = DetachedCriteria.For&amp;lt;MyCoolClass&amp;gt;()&lt;br /&gt;.Add(!Restrictions.Eq(&amp;quot;Name&amp;quot;, &amp;quot;Ramon&amp;quot;));&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;Do you see the difference? I added an exclamation mark before Restrictions.Eq to invert the restrictions operator.&lt;/p&gt;
&lt;p&gt;So now you know and probably never forget ;-)&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=483132" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/criteria+api/default.aspx">criteria api</category></item><item><title>Speeding up startup time for applications that use NHibernate</title><link>http://bloggingabout.net/blogs/ramon/archive/2010/03/30/speeding-up-startup-time-for-applications-that-use-nhibernate.aspx</link><pubDate>Tue, 30 Mar 2010 20:57:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:483040</guid><dc:creator>Ramon Smits</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/rsscomments.aspx?PostID=483040</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/commentapi.aspx?PostID=483040</wfw:comment><comments>http://bloggingabout.net/blogs/ramon/archive/2010/03/30/speeding-up-startup-time-for-applications-that-use-nhibernate.aspx#comments</comments><description>&lt;p&gt;I just read a very cool NHibernate trick to let your application start faster that was mentioned by Ricardo Peres:&lt;/p&gt;
&lt;pre&gt;Configuration cfg = new Configuration().Configure();
IFormatter serializer = new BinaryFormatter();

using (Stream stream = File.OpenWrite(&amp;quot;Configuration.serialized&amp;quot;))
{
   serializer.Serialize(stream, cfg);
}

...

using (Stream stream = File.OpenRead(&amp;quot;Configuration.serialized&amp;quot;))
{
   cfg = serializer.Deserialize(stream) as Configuration;
}
&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=483040" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/Performance/default.aspx">Performance</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/NHibernate/default.aspx">NHibernate</category></item><item><title>NHibernate bag did not delete records when IList.Clear() was called</title><link>http://bloggingabout.net/blogs/ramon/archive/2009/09/16/nhibernate-bag-did-not-delete-records-when-ilist-clear-was-called.aspx</link><pubDate>Wed, 16 Sep 2009 09:30:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:482200</guid><dc:creator>Ramon Smits</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/rsscomments.aspx?PostID=482200</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/commentapi.aspx?PostID=482200</wfw:comment><comments>http://bloggingabout.net/blogs/ramon/archive/2009/09/16/nhibernate-bag-did-not-delete-records-when-ilist-clear-was-called.aspx#comments</comments><description>&lt;p&gt;I was having a problem where NHibernate did not automatically delete childs if a collection was emptied by calling &lt;i&gt;IList.Clear()&lt;/i&gt; like in the following code example:&lt;/p&gt;
&lt;pre&gt;var s = GetSession();
var parent = s.Get&amp;lt;Parent&amp;gt;(1);
parent.Childs.Clear();&lt;/pre&gt;
&lt;p&gt;What did work ofcourse was code like the following before executing clear which marked the records for deletion and NHibernate executed the correct delete statements when the session was closed or flushed.&lt;/p&gt;
&lt;pre&gt;foreach(var c in parent.Childs) s.Delete(c);&lt;/pre&gt;
&lt;p&gt;I searched the internet for quite a while and playing around with the &lt;strong&gt;cascade&lt;/strong&gt; and &lt;strong&gt;inverse&lt;/strong&gt; attributes in the &lt;strong&gt;.hbm&lt;/strong&gt; files as I knew it had be an error in the configuration.&lt;/p&gt;
&lt;pre&gt;&amp;lt;bag name=&amp;quot;Topics&amp;quot; &lt;b&gt;cascade=&amp;quot;all&amp;quot; inverse=&amp;quot;true&amp;quot;&lt;/b&gt;&amp;gt;
	&amp;lt;key column=&amp;quot;Webcast_Id&amp;quot;/&amp;gt;
	&amp;lt;one-to-many class=&amp;quot;AddonIndex&amp;quot;/&amp;gt;
&amp;lt;/bag&amp;gt;
&lt;/pre&gt;
&lt;p&gt;After searching for quite some time I found that the problem was &lt;b&gt;cascade=&amp;quot;all&amp;quot;&lt;/b&gt;&amp;nbsp;which should have been &lt;b&gt;cascade=&amp;quot;all-delete-orphan&amp;quot;&lt;/b&gt;&amp;nbsp;and when I read that on a forum I had a very bigy WTF moment. I *really* assumed that &lt;b&gt;all&lt;/b&gt;&amp;nbsp;would do the deletion of the orphans as that is what the keyword implies, that it does *all* while in reality it does all except deletion of orphans.&lt;/p&gt;
&lt;p&gt;So I would like to suggest the (N)Hibernate team to change the names of the cascade values or just ditch the all value.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=482200" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/WTF/default.aspx">WTF</category></item><item><title>NHibernate implicit join quirk</title><link>http://bloggingabout.net/blogs/ramon/archive/2008/09/19/nhibernate-implicit-join-quirk.aspx</link><pubDate>Fri, 19 Sep 2008 09:14:28 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:474755</guid><dc:creator>Ramon Smits</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/rsscomments.aspx?PostID=474755</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/ramon/commentapi.aspx?PostID=474755</wfw:comment><comments>http://bloggingabout.net/blogs/ramon/archive/2008/09/19/nhibernate-implicit-join-quirk.aspx#comments</comments><description>&lt;p&gt;Recently I had a problem where in an HQL query the result contained duplicates. I used implicit joins in the my query and it looked like the following:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;from&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Order o&lt;br /&gt;where&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (o.Code like :literal&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR o.CustomData like :literal&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR o.Customer.Code like :literal&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR o.Customer.Name like :literal)&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;This HQL got translated to the following TSQL query:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;select &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; order0_.* -- all order columns removed for readability &lt;br /&gt;from &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nhtest.dbo.Order order0_, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nhtest.dbo.Customer customer0_ &lt;br /&gt;where &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (order0_.Code like @p0 ) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR(order0_.CustomData like @p1 ) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;OR(customer0_.Code like @p2&amp;nbsp; and order0_.CustomerId=customer0_.Id) -- Incorrect join!&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR(customer0_.Name like @p3&amp;nbsp; and order0_.CustomerId=customer0_.Id) -- Incorrect join!&lt;/strong&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The join isn&amp;#39;t correct as it should have been the following:&lt;/p&gt; &lt;blockquote&gt;select &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; order0_.* -- all order columns removed for readability &lt;br /&gt;from &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;nhtest.dbo.Order order0_, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; JOIN nhtest.dbo.Customer customer0_ ON (order0_.CustomerId=customer0_.Id)&lt;br /&gt;&lt;/strong&gt;where &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; order0_.Code like @p0 ) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR(order0_.CustomData like @p1 ) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR(customer0_.Code like @p2) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OR(customer0_.Name like @p3) &lt;br /&gt;) &lt;/blockquote&gt; &lt;p&gt;The (N)Hibernate documentation says that it is better to make use of the explicit join syntax and that is working correct. So do not use implicit joins (well, atleast not in version 2.0.0.GA)!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=474755" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/HQL/default.aspx">HQL</category><category domain="http://bloggingabout.net/blogs/ramon/archive/tags/Implicit+join/default.aspx">Implicit join</category></item></channel></rss>