<?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>Everything old is new again &#187; or mapping</title>
	<atom:link href="http://www.svendtofte.com/tag/or-mapping/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.svendtofte.com</link>
	<description>rantings &#38; scraps on code and web development</description>
	<lastBuildDate>Sat, 06 Nov 2010 20:55:12 +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>Setting up NHibernate 2.0</title>
		<link>http://www.svendtofte.com/serverside/setting-up-nhibernate-20/</link>
		<comments>http://www.svendtofte.com/serverside/setting-up-nhibernate-20/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 20:52:47 +0000</pubDate>
		<dc:creator>Svend</dc:creator>
				<category><![CDATA[Serverside]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[or mapping]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.svendtofte.com/?p=102</guid>
		<description><![CDATA[I&#8217;ve spent several long hours over the last week, trying to get Hibernate for .NET to even just work in Visual Studio. The changed configuration format has been, since I&#8217;m not seeing any real examples of how to setup NHibernate 2.0 (Beta 2, which will probably be going release later this month) on the net, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent several long hours over the last week, trying to get <a href="http://www.nhibernate.org/">Hibernate for .NET</a> to even just work in Visual Studio. The changed configuration format has been, since I&#8217;m not seeing any real examples of how to setup NHibernate 2.0 (Beta 2, which will probably be going release later this month) on the net, I&#8217;m just putting it out there now, since I&#8217;m trying to learn the whole framework, and so far this was my biggest stumbling point.</p>
<p>Important to note that this isn&#8217;t a <em>true example</em> by any measure. It merely shows what to put in your <code>App.config</code> (especially since the official documentation mostly covers the Java version, which naturally has no idea what <code>App.config</code> is. And what <a href="http://www.hibernate.org/362.html">little there is</a> for NHibernate, is for the last release. </p>
<p><span id="more-102"></span></p>
<p>The example is very simple, it mainly shows:</p>
<ul>
<li>
<p>The contents of <code>App.config</code></p>
</li>
<li>
<p>An example <code>*.hbm.xml</code> file for the sample <code>User</code> class.</p>
</li>
<li>
<p>If you should wish, the whole Visual Studio 2008 Solution file</p>
</li>
</ul>
<p>I used <a href="http://www.microsoft.com/express/vcsharp/">Visual C# 2008 Express Edition</a> and for the database I used <a href="http://www.microsoft.com/express/sql/download/default.aspx">SQL Server Express 2005</a> (make sure to get both &#8220;Microsoft SQL Server 2005 Express Edition &#8221; and  &#8220;SQL Server Management Studio Express&#8221;, the latter part is the GUI to manage the database, unless talking pure SQL into one end of a pipe satisfies you).</p>
<p><a href="http://www.svendtofte.com/wp-content/uploads/2008/07/nhibernatetest.zip">Download the entire VS 2008 Solution.</a></p>
<h2>Configs, etc</h2>
<p>The <code>App.config</code> looks like</p>
<pre>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
  &lt;configSections&gt;
    &lt;section name="hibernate-configuration"
             type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /&gt;
  &lt;/configSections&gt;
  &lt;hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" &gt;
    &lt;session-factory&gt;
      &lt;property name="dialect"&gt;
        NHibernate.Dialect.MsSql2005Dialect
      &lt;/property&gt;
      &lt;property name="connection.provider"&gt;
        NHibernate.Connection.DriverConnectionProvider
      &lt;/property&gt;
      &lt;property name="connection.driver_class"&gt;
        NHibernate.Driver.SqlClientDriver
      &lt;/property&gt;
      &lt;property name="connection.connection_string"&gt;
        Server=SVENDSDESKTOP\SQLEXPRESS;
        Database=NHibernate;
        Integrated Security=True;
      &lt;/property&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
&lt;/configuration&gt;</pre>
<p>The <code>NHibernateTest.User.hbm.xml</code>:</p>
<pre>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest"&gt;
  &lt;class name="User, NHibernateTest" table="users"&gt;
    &lt;id name="Id" column="LogonId" type="String" length="20"&gt;
      &lt;generator class="assigned" /&gt;
    &lt;/id&gt;
    &lt;property name="UserName" column="Name" type="String" length="40"/&gt;
    &lt;property name="Password" type="String" length="20"/&gt;
    &lt;property name="EmailAddress" type="String" length="40"/&gt;
    &lt;property name="LastLogon" type="DateTime"/&gt;
  &lt;/class&gt;
&lt;/hibernate-mapping&gt;</pre>
<p>And the startup code is the following.</p>
<pre>static void Main(string[] args)
{
    Configuration cfg = new Configuration();
    cfg.AddXmlFile("NHibernateTest.User.hbm.xml");

    ISessionFactory factory = cfg.BuildSessionFactory();
    ISession session = factory.OpenSession();
    ITransaction transaction = session.BeginTransaction();

    User newUser = new User();
    newUser.Id = "joe_cool";
    newUser.UserName = "Joseph Cool";
    newUser.Password = "abc123";
    newUser.EmailAddress = "joe@cool.com";
    newUser.LastLogon = DateTime.Now;

    // Tell NHibernate that this object should be saved
    session.Save(newUser);

    // commit all of the changes to the DB and close the ISession
    transaction.Commit();
    session.Close();
}</pre>
<p>Annnd, the SQL needed would be the following. Note that the database used is &#8220;NHibernate&#8221;.</p>
<pre>USE [NHibernate]
GO
/****** Object:  Table [dbo].[users]    Script Date: 07/23/2008 22:32:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[users](
	[LogonID] [nvarchar](20) NOT NULL DEFAULT ('0'),
	[Name] [nvarchar](40) NULL DEFAULT (NULL),
	[Password] [nvarchar](20) NULL DEFAULT (NULL),
	[EmailAddress] [nvarchar](40) NULL DEFAULT (NULL),
	[LastLogon] [datetime] NULL DEFAULT (NULL),
PRIMARY KEY CLUSTERED
(
	[LogonID] ASC
)WITH (PAD_INDEX  = OFF,
       STATISTICS_NORECOMPUTE  = OFF,
       IGNORE_DUP_KEY = OFF,
       ALLOW_ROW_LOCKS  = ON,
       ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.svendtofte.com/serverside/setting-up-nhibernate-20/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

