6 Replies - 3457 Views - Last Post: 29 March 2010 - 01:32 PM Rate Topic: -----

#1 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

NHibernate Problem

Posted 26 March 2010 - 07:49 AM

I have been tryingh to learn to use NHibernate in C# for a database program. I have started small, and have been struggling to get it set up properly. Any help on the error I am receiving would be greatly appreciated.

Thanks to any who answer.

Ed

The error is as follows:

Message="could not load an entity: [BurrSOFT.Domain.Employee#1][SQL: SELECT employee0_.Id as Id0_0_, employee0_.FirstName as FirstName0_0_, employee0_.LastName as LastName0_0_ FROM Employee employee0_ WHERE employee0_.Id=?]"
Source="NHibernate"

My database is a MySql database with the following setup:

DatabaseName: BurrSOFT
Table: employees
Fields: Id - Type = int
********FirstName - Type = tinytext
********LastName - Type = tinytext

My Classes are set up in the following file structure:
BurrSOFT
|---Domain
| |-Employee.cs
| |-IEmployeeRepository.cs
|
|---Mappings
| |-Employee.htm.xml
|
|---Repositories
| |-EmployeeRepository.cs
| |-NHibernateHelper.cs
|
|---Form1.cs
|
|---hibernate.cfg.xml
|
|---program.cs

My Employee Class:

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

namespace BurrSOFT.Domain
{
    public class Employee
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    }
}



My Mapping File:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BurrSOFT"
                   namespace="BurrSOFT.Domain">
  <class name="BurrSOFT.Domain.Employee" lazy="true">
    <id name="Id">
      <generator class="native" />
    </id>
    <property name="FirstName" column="FirstName"  />
    <property name="LastName" column="LastName" />
  </class>
</hibernate-mapping>


My Configuration File:
<?xml version="1.0" encoding="utf-8" ?>
<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.MySqlDataDriver</property>
    <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    <property name="connection.connection_string">
      Database=burrsoft;Data Source=localhost;User Id=root;Password=jsb1srb2
    </property>
    <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle
    </property>
  </session-factory>
</hibernate-configuration>


My NHibernate Helper File:

using BurrSOFT.Domain;
using NHibernate;
using NHibernate.Cfg;

namespace BurrSOFT.Repositories
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly(typeof(Employee).Assembly);
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}



My Repository Files:
Interface:
sing System;
using System.Collections.Generic;

namespace BurrSOFT.Domain
{
    public interface IEmployeeRepository
    {
        void Add(Employee employee);
        void Update(Employee employee);
        void Remove(Employee employee);
        Employee GetById(int employeeId);
        ICollection<Employee> GetByLastName(string lastName);
    }
}


EmployeeRepository:

using System;
using System.Collections.Generic;
using BurrSOFT.Domain;
using NHibernate;

namespace BurrSOFT.Repositories
{
    public class EmployeeRepository : IEmployeeRepository
    {
        public void Add(Employee employee)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(employee);
                transaction.Commit();
            }
        }

        public void Update(Employee employee)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(employee);
                transaction.Commit();
            }
        }

        public void Remove(Employee employee)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(employee);
                transaction.Commit();
            }
        }
        
        public Employee GetById(int employeeId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<Employee>(employeeId);
        }

        public ICollection<Employee> GetByLastName(string lastName)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var employees = session
                    .CreateCriteria(typeof(Employee))
                    .Add(NHibernate.Criterion.Restrictions.Eq("LastName", lastName))
                    .List<Employee>();
                return employees;
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: NHibernate Problem

#2 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: NHibernate Problem

Posted 29 March 2010 - 09:55 AM

Hi all,

I tried posting this to the Databases forum, and got no answer, so I am hoping that I migyht get a better response here.

Here is the issue:

I have been tryingh to learn to use NHibernate in C# for a database program. I have started small, and have been struggling to get it set up properly. Any help on the error I am receiving would be greatly appreciated.

Thanks to any who answer.

Ed

The error is as follows:

Message="could not load an entity: [BurrSOFT.Domain.Employee#1][SQL: SELECT employee0_.Id as Id0_0_, employee0_.FirstName as FirstName0_0_, employee0_.LastName as LastName0_0_ FROM Employee employee0_ WHERE employee0_.Id=?]"
Source="NHibernate"

My database is a MySql database with the following setup:

DatabaseName: BurrSOFT
Table: employees
Fields: Id - Type = int
********FirstName - Type = tinytext
********LastName - Type = tinytext

My Classes are set up in the following file structure:
BurrSOFT
|---Domain
| |-Employee.cs
| |-IEmployeeRepository.cs
|
|---Mappings
| |-Employee.htm.xml
|
|---Repositories
| |-EmployeeRepository.cs
| |-NHibernateHelper.cs
|
|---Form1.cs
|
|---hibernate.cfg.xml
|
|---program.cs

My Employee Class:

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

namespace BurrSOFT.Domain
{
    public class Employee
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    }
}



My Mapping File:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BurrSOFT"
                   namespace="BurrSOFT.Domain">
  <class name="BurrSOFT.Domain.Employee" lazy="true">
    <id name="Id">
      <generator class="native" />
    </id>
    <property name="FirstName" column="FirstName"  />
    <property name="LastName" column="LastName" />
  </class>
</hibernate-mapping>


My Configuration File:

<?xml version="1.0" encoding="utf-8" ?>
<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.MySqlDataDriver</property>
    <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    <property name="connection.connection_string">
      Database=burrsoft;Data Source=localhost;User Id=root;Password=jsb1srb2
    </property>
    <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle
    </property>
  </session-factory>
</hibernate-configuration>



My NHibernate Helper File:

using BurrSOFT.Domain;
using NHibernate;
using NHibernate.Cfg;

namespace BurrSOFT.Repositories
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly(typeof(Employee).Assembly);
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}



My Repository Files:
Interface:

using System;
using System.Collections.Generic;

namespace BurrSOFT.Domain
{
    public interface IEmployeeRepository
    {
        void Add(Employee employee);
        void Update(Employee employee);
        void Remove(Employee employee);
        Employee GetById(int employeeId);
        ICollection<Employee> GetByLastName(string lastName);
    }
}



EmployeeRepository:

using System;
using System.Collections.Generic;
using BurrSOFT.Domain;
using NHibernate;

namespace BurrSOFT.Repositories
{
    public class EmployeeRepository : IEmployeeRepository
    {
        public void Add(Employee employee)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(employee);
                transaction.Commit();
            }
        }

        public void Update(Employee employee)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(employee);
                transaction.Commit();
            }
        }

        public void Remove(Employee employee)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(employee);
                transaction.Commit();
            }
        }
        
        public Employee GetById(int employeeId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<Employee>(employeeId);
        }

        public ICollection<Employee> GetByLastName(string lastName)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var employees = session
                    .CreateCriteria(typeof(Employee))
                    .Add(NHibernate.Criterion.Restrictions.Eq("LastName", lastName))
                    .List<Employee>();
                return employees;
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: NHibernate Problem

Posted 29 March 2010 - 10:37 AM

Please upload a zip of your entire project. This is way too hard to diagnose by just looking at it.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: NHibernate Problem

Posted 29 March 2010 - 11:41 AM

Attached are the files from my project. I have also placed libmysqld.dll in my shared library folder, but it is to large to zip and post with my 2MB limit. :(

Thanks for the help Mental!

Ed.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: NHibernate Problem

Posted 29 March 2010 - 11:47 AM

I merged your topics. Please don't post duplicate question in multiple forums :)
Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: NHibernate Problem

Posted 29 March 2010 - 12:03 PM

Well, it would seem that you simply do not have a table in your database that meets the requirements of your mapping.

I reproduced the error without having created the table. Then I created the table as Id INT Identity, FirstName NVARCHAR(50), LastName NVARCHAR(50) and it works fine. There is of course the question of the employee existing in the database. So, at this section:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            IEmployeeRepository repository = new EmployeeRepository();
            var fromDb = repository.GetById(1);

            ui_EmployeeIdLabel.Text = fromDb.Id.ToString();
            ui_FirstNameTextBox.Text = fromDb.FirstName;
            ui_LastNameTextBox.Text = fromDb.LastName;
        }

        
    }



You'll want to check for null record and in such case, not use your fromDb (bad name by the way) member. Something like this should work:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            IEmployeeRepository repository = new EmployeeRepository();
            
            int employeeId = 1;
            var fromDb = repository.GetById(employeeId);

            if (fromDb != null)
            {
                ui_EmployeeIdLabel.Text = fromDb.Id.ToString();
                ui_FirstNameTextBox.Text = fromDb.FirstName;
                ui_LastNameTextBox.Text = fromDb.LastName;
            }
            else
            {
                MessageBox.Show(String.Format("No employee with id of {0} was found.",employeeId), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ui_EmployeeIdLabel.Text = string.Empty;
                ui_FirstNameTextBox.Text = string.Empty;
                ui_LastNameTextBox.Text = string.Empty;
            }
        }

        
    }



So, step one: ensure your table even exists. Then step two is to make your form instantiation safer.
Was This Post Helpful? 0
  • +
  • -

#7 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: NHibernate Problem

Posted 29 March 2010 - 01:32 PM

Ok, I figured it out! Some idoit (most likely me since no one else uses my computer) went and named the table employees! I changed the table name to employee, and everything works wonderfully...

Thanks Mental!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1