"NHibernate Part 1: Hello World with NHibernate" article - Getting to work in Microsoft SQL Server 2005

You can check out this article here http://sdesmedt.wordpress.com/2006/04/05/hello-world-with-nhibernate/

I can say it is really easy to get started. However if you follow the codes in there, you will start to realize to face problems getting it running.

I will provide some tips to get it work.

1)      Requires Virtual on each property

As on original article:

public class MessageProvider

{

    private int id;

    private string message;

 

    public MessageProvider()

    {

 

    }

 

    public int Id

    {

        get { return id; }

        set { id = value; }

    }

 

  

    public string Message

    {

        get { return message; }

        set { message = value; }

    }

}

Errors that you will get:

The following types may not be used as proxies:

TheData.MessageProvider: method get_Message should be virtual

TheData.MessageProvider: method get_Id should be virtual

TheData.MessageProvider: method set_Id should be virtual

TheData.MessageProvider: method set_Message should be virtual

Solution:

public class MessageProvider

{

    private int id;

    private string message;

 

    public virtual int Id

    {

        get { return id; }

        set { id = value; }

    }

 

  

    public virtual string Message

    {

        get { return message; }

        set { message = value; }

    }

}

You can remove the empty constructor. There is no reason to keep it there too.

2)      Hibernate-Mapping namespace

I check in search engine and I realize a lot of people are frustrated about this. Easy. If you are using NHibernate 1.2.0.4000

As on original article:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">

Solution:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">

Change it from 2.0 to 2.2. It solves the problem.

3)      If you are using Microsoft SQL Server 2005

As on original article:

config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MySqlDialect");

Solution:

config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2005Dialect");

Just to make sure you have the right settings for your database, do check at this link http://www.hibernate.org/361.html?cmd=comphist&histnode=2604

There is no updates for Microsoft SQL Server 2005 but it worked the same with Microsoft SQL Server 2000.

Just to add that you can do this:

config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;Integrated Security=SSPI;");

 

config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;User ID=sa;Password=sa;");

The 2nd one is not part of the url above. But it will work just fine J

The connection string is very important especially to execute the code below:

IList messages = session.CreateCriteria(typeof(TheData.MessageProvider)).List();

4)      How do you name the hbm.xml file?

You can name it anything that you want. As for this case, I have only one mapping file and a MessageProvider class. I will call it MessageProvider.hbm.xml.

Below is the actual source code:

** Take note, I will base on that article. So I have not implement any best practices. So readers who face problem from that article can refer it here.

MessageProvider.cs

using System;

using System.Collections.Generic;

using System.Text;

 

namespace TheData

{

    public class MessageProvider

    {

        private int id;

        private string message;

 

        public virtual int Id

        {

            get { return id; }

            set { id = value; }

        }

 

        public virtual string Message

        {

            get { return message; }

            set { message = value; }

        }  

    }

}

MessageProvider.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">

      <class name="TheData.MessageProvider, HelloNHibernate" table="HelloWorld">

            <id name="Id">

                  <generator class="identity" />

            </id>

            <property name="Message" />

      </class>

</hibernate-mapping>

Program.cs (or MainClass.cs)

using System;

using System.Collections;

using NHibernate;

using NHibernate.Cfg;

 

namespace HelloNHibernate

{

    class Program

    {

        static void Main(string[] args)

        {

            Configuration config = new Configuration();

            config.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");

            config.SetProperty(NHibernate.Cfg.Environment.Dialect, "NHibernate.Dialect.MsSql2005Dialect");

            //config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;Integrated Security=SSPI;");

            config.SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"Server=wenchingpc\sql2005;initial catalog=nhibernate_test;User ID=sa;Password=!pandu@;");

            config.AddAssembly("HelloNHibernate");

 

            ISessionFactory factory = config.BuildSessionFactory();

 

            try

            {

                ISession session = factory.OpenSession();

                IList messages = session.CreateCriteria(typeof(TheData.MessageProvider)).List();

 

                Console.WriteLine("No Messages: {0}\n", messages.Count);

 

                foreach (TheData.MessageProvider aMessage in messages)

                {

                    Console.WriteLine("Id: {0} - Message: {1}", aMessage.Id, aMessage.Message);

                }

 

                session.Close();

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

 

            Console.Read();

        }

    }

}

 

Hope you find it useful.  Thank you.

Published Friday, September 21, 2007 9:18 PM by chuawenching
Filed under: ,

Comments

# re: "NHibernate Part 1: Hello World with NHibernate" article - Getting to work in Microsoft SQL Server 2005

Saturday, September 22, 2007 8:23 AM by cruizer

if only "virtual" was default, we wouldn't have problems with inheritance and stuff ;) is there any performance drawback to making all methods/properties virtual?

# re: "NHibernate Part 1: Hello World with NHibernate" article - Getting to work in Microsoft SQL Server 2005

Saturday, September 22, 2007 11:07 AM by chuawenching

Hi. I doubt there will be. However you can switch if off.

You can read this article by Billy McCafferty

http://www.codeproject.com/aspnet/NHibernateBestPractices.asp?df=100&forumid=278860&exp=0&select=2204966#xx2204966xx

Under Migrating from NHibernate 1.0x to 1.2:

All classes and collections with 1.2 are now loaded lazily by default. Consequently, if you run your application without modifying the lazy attribute, you'll most likely receive a number of "method x should be virtual." To resolve this, set lazy="false" for each class and collection that you do not want loaded lazily.

If you look at nhibernate doc, I doubt there will be issues too. http://www.hibernate.org/hib_docs/nhibernate/html/performance.html

# re: "NHibernate Part 1: Hello World with NHibernate" article - Getting to work in Microsoft SQL Server 2005

Tuesday, September 25, 2007 6:50 PM by woaychee

will be more challenging if you try to join with multiple tables (trust me). do try out doodads and easy object too.

# re: "NHibernate Part 1: Hello World with NHibernate" article - Getting to work in Microsoft SQL Server 2005

Tuesday, September 25, 2007 7:15 PM by cruizer

well, joining is not a strong suit of objects...this is a task that's more well-suited for tabular data. i guess in this case HQL will be nice...

# re: "NHibernate Part 1: Hello World with NHibernate" article - Getting to work in Microsoft SQL Server 2005

Wednesday, September 26, 2007 9:47 AM by chuawenching

True.

Anyway the hard part is to write that hbm.xml file. However thanks to Ayende, that he created the http://www.ayende.com/projects/nhibernate-query-analyzer.aspx

I am still exploring on HQL and probably the support for LINQ on NHibernate. I heard for doodads. Will give it a try.

# Hello World With Hibernate - Global Point Forum

Sunday, August 31, 2008 5:35 PM by Hello World With Hibernate - Global Point Forum

Pingback from  Hello World With Hibernate - Global Point Forum