NHibernate mapping error SQL Server 2008 Express

Posted by developer on Stack Overflow See other posts from Stack Overflow or by developer
Published on 2010-04-14T20:07:58Z Indexed on 2010/04/14 20:43 UTC
Read the original article Hit count: 479

Hi All,

I tried an example from NHibernate in Action book and when I try to run the app, it throws an exception saying "Could not compile the mapping document:

HelloNHibernate.Employee.hbm.xml"

Below is my code,

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="HelloNHibernate.Employee, HelloNHibernate" lazy="false" table="Employee">
<id name="id" access="field">
  <generator class="native"/>
</id>
<property name="name" access="field" column="name"/>
<many-to-one access="field" name="manager" column="manager" cascade="all"/>
</class>

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using System.Reflection;
using NHibernate.Cfg;

namespace HelloNHibernate
{
class Program
{
    static void Main(string[] args)
    {
        CreateEmployeeAndSaveToDatabase();
        UpdateTobinAndAssignPierreHenriAsManager();
        LoadEmployeesFromDatabase();

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    static void CreateEmployeeAndSaveToDatabase()
    {
        Employee tobin = new Employee();
        tobin.name = "Tobin Harris";

        using (ISession session = OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(tobin);
                transaction.Commit();
            }
            Console.WriteLine("Saved Tobin to the database");
        }
    }

    static ISession OpenSession()
    {
        if (factory == null)
        {
            Configuration c = new Configuration();
            c.AddAssembly(Assembly.GetCallingAssembly());
            factory = c.BuildSessionFactory();
        }
        return factory.OpenSession();
    }

    static void LoadEmployeesFromDatabase()
    {
        using (ISession session = OpenSession())
        {
            IQuery query = session.CreateQuery("from Employee as emp order by emp.name asc");

            IList<Employee> foundEmployees = query.List<Employee>();

            Console.WriteLine("\n{0} employees found:", foundEmployees.Count);

            foreach (Employee employee in foundEmployees)
                Console.WriteLine(employee.SayHello());
        }
    }

    static void UpdateTobinAndAssignPierreHenriAsManager()
    {
        using (ISession session = OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                IQuery q = session.CreateQuery("from Employee where name='Tobin Harris'");

                Employee tobin = q.List<Employee>()[0];
                tobin.name = "Tobin David Harris";

                Employee pierreHenri = new Employee();
                pierreHenri.name = "Pierre Henri Kuate";

                tobin.manager = pierreHenri;
                transaction.Commit();

                Console.WriteLine("Updated Tobin and added Pierre Henri");
            }
        }
    }

    static ISessionFactory factory;
}
}

Employee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloNHibernate
{
class Employee
{
    public int id;
    public string name;
    public Employee manager;

    public string SayHello()
    {
        return string.Format("'Hello World!', said {0}.", name);
    }
}
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" 
             type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>        
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
     <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">
        Data Source=SQLEXPRESS2008;Integrated Security=True; User ID=SQL2008;Password=;initial catalog=HelloNHibernate
       </property>
       <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
       <property name="show_sql">false</property>
     </session-factory>
   </hibernate-configuration>
</configuration>

© Stack Overflow or respective owner

Related posts about nhibernate-mapping

Related posts about sql-server-2008-express