<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>NHibernate</title>
        <link>http://geekswithblogs.net/jwhitehorn/category/6770.aspx</link>
        <description>NHibernate</description>
        <language>en-US</language>
        <copyright>Jason Whitehorn</copyright>
        <managingEditor>jason.whitehorn@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>NHibernate Criteria Queries</title>
            <link>http://geekswithblogs.net/jwhitehorn/archive/2007/06/30/113603.aspx</link>
            <description>&lt;strong&gt;UPDATE (12/17/2007):&lt;/strong&gt; My blog has moved. This post is now located at: &lt;a href="http://jason.whitehorn.ws/2007/06/30/NHibernate+Criteria+Queries.aspx"&gt;http://jason.whitehorn.ws/2007/06/30/NHibernate+Criteria+Queries.aspx&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;p&gt;&lt;a href="http://www.nhibernate.org"&gt;NHibernate's&lt;/a&gt; support for criteria queries provides an extremely powerful query feature into an easy to use package. NHibernate has a rather complete manual that covers criteria queries, which you can checkout &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/querycriteria.html"&gt;here.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The NHibernate documentation has a ton of information packed within it, and can be a little overwhelming for those who are new to NHibernate. So, as an argumentation to the documentation for criteria searching, I will provide a really basic overview of how things work.&lt;/p&gt;
&lt;p&gt;The NHibernate session exposes a method called "CreateCriteria", which accepts a Type and returns an ICriteria. This is the starting point for any criteria query. The Type you pass the method, is the same Type that you are querying for. For example, if you have a business object call "Customer" that we want to search for using criteria, you would:&lt;/p&gt;
&lt;br /&gt;
&lt;pre class="code"&gt;ICriteria crit = session.CreateCriteria(typeof(Customer));&lt;/pre&gt;
&lt;br /&gt;
&lt;p&gt;Now that you have created an ICriteria object to represent your search, you need to start specifying various criteria for the object NHibernate will fetch. This is were the "Add" method comes in handy. &lt;/p&gt;
&lt;p&gt;The Add method accepts any ICriterion object, but to help you along NHibernate provides the "Expression" object which has many static methods to help you form your criteria. Continuing with our running example, if you want to select customers who are older than 21 years of age you would:&lt;/p&gt;
&lt;br /&gt;
&lt;pre class="code"&gt;crit.Add(Expression.Gt("Age", 21));&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;p&gt;The above code tell NHibernate that the "Age" property of our Customer object needs to be greater than the value 21. In the above manor, you can use the Add method and the Expression object to specify exactly which objects you want to retrieve.&lt;/p&gt;
&lt;p&gt;To actually fetch our objects from the database, you execute the "List" method on the ICriteria object.&lt;/p&gt;
&lt;br /&gt;
&lt;pre class="code"&gt;IList&lt;customer&gt; results = crit.List&lt;customer&gt;();&lt;br /&gt;&lt;/customer&gt;&lt;/customer&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;p&gt;There are many more capabilities of criteria queries in NHibernate that I have not even touched upon, which you can read more about in the official NHibernate documentation. &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113603"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113603" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/jwhitehorn/aggbug/113603.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jason Whitehorn</dc:creator>
            <guid>http://geekswithblogs.net/jwhitehorn/archive/2007/06/30/113603.aspx</guid>
            <pubDate>Sat, 30 Jun 2007 18:34:44 GMT</pubDate>
            <comments>http://geekswithblogs.net/jwhitehorn/archive/2007/06/30/113603.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/jwhitehorn/comments/commentRss/113603.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/jwhitehorn/services/trackbacks/113603.aspx</trackback:ping>
        </item>
        <item>
            <title>NHibernate and Text fields</title>
            <link>http://geekswithblogs.net/jwhitehorn/archive/2006/10/11/93821.aspx</link>
            <description>&lt;strong&gt;UPDATE (12/17/2007):&lt;/strong&gt; My blog has moved. This post is now located at: &lt;a href="http://jason.whitehorn.ws/2006/10/12/NHibernate+And+Text+Fields.aspx"&gt;http://jason.whitehorn.ws/2006/10/12/NHibernate+And+Text+Fields.aspx&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In my &lt;a href="http://geekswithblogs.net/jwhitehorn/archive/2006/08/22/88884.aspx"&gt;last post about NHibernate&lt;/a&gt; I mentioned using &lt;a href="http://wiki.nhibernate.org/display/NHC/NHibernate.Mapping.Attributes"&gt;NHibernate.Mapping.Attributes&lt;/a&gt; to simplify things. &lt;br /&gt;
Since my last post I have had the need to store rather large text data in a SQL Server Text field. My initial attempt was to simply create a C# string property that represented the field as such:&lt;br /&gt;
&lt;pre class="code"&gt;        [NHMA.Property(Name="SomeData")]&lt;br /&gt;        public string SomeData {&lt;br /&gt;            get { return _someData; }&lt;br /&gt;            set { _someData = value; }&lt;br /&gt;        }&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
I was shocked to learn that the above did not work correctly. What I was experiencing was NHibernate was silently truncating my data to 3000 characters (or maybe it was 4000, I can't remember exactly). The reason for this appears to be that NHibernate assumes the field type to be of varchar not Text.&lt;br /&gt;
&lt;br /&gt;
The solution to this problem is to inform NHibernate of the field's true type. As it turns out this can be done by specifying it as type 'StringClob'.&lt;br /&gt;
&lt;pre class="code"&gt;        [NHMA.Property(Name="SomeData", Type="StringClob")]&lt;br /&gt;        public string SomeData {&lt;br /&gt;            get { return _someData; }&lt;br /&gt;            set { _someData = value; }&lt;br /&gt;        }&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
After telling NHibernate that the field was of type StringClob it no longer truncates large strings, and correctly treated it as a Text field.&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=93821"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=93821" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/jwhitehorn/aggbug/93821.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jason Whitehorn</dc:creator>
            <guid>http://geekswithblogs.net/jwhitehorn/archive/2006/10/11/93821.aspx</guid>
            <pubDate>Thu, 12 Oct 2006 04:57:00 GMT</pubDate>
            <comments>http://geekswithblogs.net/jwhitehorn/archive/2006/10/11/93821.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/jwhitehorn/comments/commentRss/93821.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/jwhitehorn/services/trackbacks/93821.aspx</trackback:ping>
        </item>
        <item>
            <title>NHibernate</title>
            <link>http://geekswithblogs.net/jwhitehorn/archive/2006/08/22/88884.aspx</link>
            <description>&lt;strong&gt;UPDATE (12/17/2007):&lt;/strong&gt; My blog has moved. This post is now located at: &lt;a href="http://jason.whitehorn.ws/2006/08/23/NHibernate.aspx"&gt;http://jason.whitehorn.ws/2006/08/23/NHibernate.aspx&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Today was my first real day playing with &lt;a href="http://www.nhibernate.org/"&gt;NHibernate&lt;/a&gt;. &lt;br /&gt;
The &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/nhibernate_reference.pdf"&gt;documentation&lt;/a&gt; is a little much for a beginner such as myself, but I managed to struggle through it... especially with the help of my coworkers.&lt;br /&gt;
&lt;br /&gt;
For those of you who don't know, NHibernate is an Object/Relation Mapper (ORM). It classically uses XML files as meta-data to help with its job. I personally did not like the idea of having embedded XML resources in my project, so I was very pleased when I found the &lt;a href="http://wiki.nhibernate.org/display/NHC/NHibernate.Mapping.Attributes"&gt;NHibernate.Mapping.Attributes&lt;/a&gt; package that allows me to use native C# attribute tagging instead of embedded XML.&lt;br /&gt;
&lt;br /&gt;
One thing that was not entirely obvious to me was that, despite NHibernate 1.0.2 being a .NET 1.1 resource, it does support nullable types (which were first introduced in .NET 2.0). Say, for example, you have a DateTime in your database that is specified as a nullable field. In your container object you have a DateTime? (notice the question mark). To make NHibernate work with this you will have to do two things.&lt;br /&gt;
&lt;br /&gt;
1) Specify in your NHibernate attributes the C# type (in this case only Specify System.DateTime, ignoring the nullable)&lt;br /&gt;
2) Specify in your NHibernate attributes that NotNull is equal to false (ie, it is nullable)&lt;br /&gt;
&lt;br /&gt;
That's it! That seems to be all I had to do to get NHibernate 1.0.2 to work with nullable types!&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=88884"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=88884" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/jwhitehorn/aggbug/88884.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jason Whitehorn</dc:creator>
            <guid>http://geekswithblogs.net/jwhitehorn/archive/2006/08/22/88884.aspx</guid>
            <pubDate>Wed, 23 Aug 2006 04:04:00 GMT</pubDate>
            <comments>http://geekswithblogs.net/jwhitehorn/archive/2006/08/22/88884.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/jwhitehorn/comments/commentRss/88884.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/jwhitehorn/services/trackbacks/88884.aspx</trackback:ping>
        </item>
    </channel>
</rss>