views:

146

answers:

3

Hi,

I am trying to configure NHibernate on my console application in which I am trying to map an Employee class to a table Employee.

I am getting below exception which I have no clue:

Class:

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

        }
    }

TABLE:

CREATE TABLE Employee (
id int identity primary key,
name varchar(50),
manager int )
GO

Mapping File:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
auto-import="true">
  <class name="ConsoleApplication.Employee, ConsoleApplication1" lazy="false">
    <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>
</hibernate-mapping>

App.config

<?xml version="1.0" ?>
<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="proxyfactory.factory_class">
        NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
      </property>

      <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
      </property>
      <property name="connection.connection_string">
        Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AAM;Data Source=(local)
      </property>
      <property name="dialect">
        NHibernate.Dialect.MsSql2005Dialect
      </property>
      <property name="show_sql">
        false
      </property>
    </session-factory>
  </hibernate-configuration>
</configuration>

Error:

Message: could not execute query
[ select employee0_.id as id0_, employee0_.name as name0_, employee0_.manager as manager0_ from Employee employee0_ ]
[SQL: select employee0_.id as id0_, employee0_.name as name0_, employee0_.manager as manager0_ from Employee employee0_]

StackTrace:   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
   at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
   at NHibernate.Impl.SessionImpl.List(String query, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.SessionImpl.List[T](String query, QueryParameters parameters)
   at NHibernate.Impl.QueryImpl.List[T]()
   at ConsoleApplication.Program.LoadEmployeesFromDatabase() in C:\Mahesh\Code\HelloNHibernate\ConsoleApplication1\Program.cs:line 50
   at ConsoleApplication.Program.Main(String[] args) in C:\Mahesh\Code\HelloNHibernate\ConsoleApplication1\Program.cs:line 18

Inner Exception: Invalid object name 'Employee'

I have the Employee in my database and when I run the query which runtime given in the message property executing successfully.

Any thoughts on this??

Thanks, Mahesh

A: 

I suspect that the problem might be with the reference to the Manager rather than the top level Employee class. Does your mapping have a refernce to the Manager as a class rather than as a value? It appears from the generated SQL that you don't

Daniel Dyson
A: 

The problem is in your many-to-one mapping of the manager field. You have specified:

access="field"

This tells NHibernate to use reflection to determine the name of the field in your database. You have a couple of options, but in your case I think it will be safe just to remove the access attribute which will mean that NHibernate will use its default access (property) which will then use manager as you have specified this as the name of the property. If this doesn't meet your requirements then do some reading around the field attribute to see the other options you have.

s1mm0t
A: 

I understood that NHibernate cannot map to internal classes.

Add the public scope declaration to your Employee class.

Neil Moss