Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I've started working with NHibernate for the first time and doing with some reference given in Code Project. I have created a console application which does simple work employee details maintenance. Table Employee has ID and Name as columns, I'm deleting, inserting and updatinh through Nhibernate. But I'm facing an issue or exception Object Reference not set to an instance of an object

HEre is wat I've done so far


Employee.cs

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

namespace NHibernateSimpleDemo
{
    class Employee
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }


    }
}


Employee.hbm.xml

XML
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateSimpleDemo" assembly="NHibernateSimpleDemo">
  <class name="Employee" table="Employee" lazy="false">
    <id name="ID" column="ID">
      <column name ="ID"/>
      <generator class="native"/>
    </id>
    <property name="Name" column="Name"  />
  </class>
</hibernate-mapping>


App config file

XML
<?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="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Data Source=(local);Initial Catalog=Testing;User ID=sa;Password=Keane123"</property>
      <property name="show_sql">true</property>
      <mapping assembly="NHibernateSimpleDemo" />
    </session-factory>
  </hibernate-configuration>
</configuration>

<!--http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=222279-->



Program.cs file

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;

namespace NHibernateSimpleDemo
{
    class Program
    {
        private static Configuration myConfiguration;
        private static ISessionFactory mySessionFactory = null;
        private static ISession mySession;

        static void Main(string[] args)
        {
            try
            {
                if (mySession != null && mySession.IsOpen)
                {
                    mySession.Close();
                }
                if (mySessionFactory != null && !mySessionFactory.IsClosed)
                {
                    mySessionFactory.Close();
                }
                Employee emp = new Employee();
                //emp.ID = 1;
                //emp.Name = "Admin";
                myConfiguration = new Configuration();
                myConfiguration.Configure();
                myConfiguration.AddAssembly("NHibernateSimpleDemo");
                mySessionFactory = myConfiguration.BuildSessionFactory();
                mySession = mySessionFactory.OpenSession();
                Console.WriteLine("Insert an employee");
                Console.WriteLine("Enter employee ID");
                emp.ID = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("enter name of employee");
                emp.Name = Console.ReadLine();
                using (mySession.BeginTransaction())
                {
                    mySession.Save(emp.ID, emp.Name);
                    mySession.Transaction.Commit();
                }
                Console.WriteLine("Update an employee");
                using (mySession.BeginTransaction())
                {
                    var empl = mySession.Load<Employee>(4);
                    empl.Name = "User";
                    mySession.Update(empl);
                    mySession.Transaction.Commit();
                }
                using (mySession.BeginTransaction())
                {

                    Console.WriteLine("Delete an employee");
                    Console.WriteLine("Enter employee ID");
                    emp.ID = Convert.ToInt32(Console.ReadLine());

                    Object result = mySession.CreateSQLQuery("DELETE from Employee where ID = :itm")
                                .AddScalar("count", NHibernate.NHibernateUtil.Int32)
                                .SetParameter("itm",emp.ID)
                            .UniqueResult();
                    mySession.Transaction.Commit();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + e.InnerException.Message);
            }
        }
    }
}


I'm getting this exception when the break point reaches
mySessionFactory = myConfiguration.BuildSessionFactory();




Someone help me I'm really breaking my heads for solution

Thanks in advance
Posted
Comments
Mehdi Gholam 12-Sep-11 11:50am    
What is your exception?
CyborgForever 13-Sep-11 2:55am    
Could not compile the mapping document: NHibernateSimpleDemo.Employee.hbm.xml
"Duplicate class/entity mapping NHibernateSimpleDemo.Employee"

1 solution

may be class is declared as a public
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900