January 26, 2021
Hot Topics:

Using NHibernate as an ORM Solution for .NET

  • By David Consdorf
  • Send Email »
  • More Articles »

For the database in this example, I used MS SQL Express Edition (the MDF is included in the sample code). Any database can be used, though, because all it takes to change databases in NHibernate is a simple configuration change. Here is the database model:

Click here for a larger image.

Figure 1: NHibernate Sample Application—Database Design

NHibernate Configuration

To start using NHibernate, you will need to go to www.hibernate.org and download the latest release. Then, you need to copy the assemblies to your /bin folder and add the following configuration settings to your web.config file:

   <section name="nhibernate"
                  System, Version=1.0.5000.0, Culture=neutral,
   <section name="log4net"
   <add key="hibernate.connection.provider"
        value="NHibernate.Connection.DriverConnectionProvider" />
   <add key="hibernate.dialect"
        value="NHibernate.Dialect.MsSql2000Dialect" />
   <add key="hibernate.connection.driver_class"
        value="NHibernate.Driver.SqlClientDriver" />
   <add key="hibernate.connection.connection_string" 
        value="{Your Connection String Here}" />
   <add key="hibernate.connection.isolation"
        value="ReadCommitted" />
   <add key="hibernate.show_sql" value="True" />
   <appender name="NHibernateFileLog"
      <file value="C:/Logs/nhibernate.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1000KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%d{HH:mm:ss.fff}
                                   [%t] %-5p %c - %m%n" />
   <logger name="NHibernate" additivity="false">
      <level value="DEBUG"/>
      <appender-ref ref="NHibernateFileLog"/>

Listing 2: Sample Application—NHibernate Configuration

These NHibernate configurations control the database settings and any optional NHibernate settings you may want to set. You will notice the configuration calls for a connection provider, dialect, and driver. There is also a line for a standard connection string. In this example, everything is set up for MS SQL, but this configuration could just as easily be set up for DB2, Oracle, MySQL, and so forth.

Also note that the log4net settings are completely optional, but I highly recommend them. NHibernate is designed to work with log4net and will log what NHibernate is doing internally. Most importantly, all database activity, including the SQL is logged, so you can keep track of potential performance issues related to the type and quantity of SQL queries being run by NHibernate.

NHibernate Mapping

Now that the basic NHibernate configuration is set up, you can map your application's business objects. NHibernate uses XML files to map the objects to the database. Each object (or collection of objects) has an associated mapping file. For example, the ProductOrder class is paired with the ProductOrder.hbm.xml file. The contents of this file define how NHibernate maps each of the object's properties to a database column, how the object is identified, and any associations with other objects.

It is important to note that there are many more mapping options with NHibernate than shown here, especially with regard to collections. With the many ways that a database and object structure may be configured, NHibernate has done a pretty good job of providing flexible mapping schemes for pretty much any situation. This example will just cover the basics and touch on a few of the more interesting mapping options.

It is also worth noting that there are several automated tools provided on the NHibernate web site that help automate the process of linking the object model to the database. There are tools that automatically create mapping files and classes from a database definition. There also are tools that let you start with a mapping file and automatically create the database definition and class files. For this example, though, I will take you through the process of creating the mapping files manually with the assumption that the class files and database are already created.

ProductOrder Class Mapping—Simple Example

Take a look at the ProductOrder.hbm.xml mapping file for the ProductOrder class:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

   <class name="ProductOrder, __Code" table="ProductOrder">

      <id name="ProductOrderID" column="ProductOrderID"
          type="Int32" unsaved-value="null">
         <generator class="native" />

      <property name="CustomerID" column="CustomerID" type="Int32" />
      <property name="ProductName" column="ProductName"
                type="String" />
      <property name="Quantity" column="Quantity" type="Int32" />
      <property name="TotalCost" column="TotalCost" type="Decimal" />

Listing 3: Sample Application—ProductOrder Mapping

Page 2 of 6

This article was originally published on November 6, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date