Mapping in configuration file when using Nhibernate's mapping by code

2.3k Views Asked by At

I want to use mapping by code so I have a class Employee (namespace NHibernateTests.Classes) and a class EmployeeMappings (namespace NHibernateTests.Mappings)

My whole nhibernate configuration is set in an xml file hibernate.cfg.xml which currently goes like this:

<?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.OracleClientDriver</property>
    <property name="connection.connection_string">User Id=NHIBERNATE;Password=NHIBERNATE;Data Source=XE</property>
    <property name="show_sql">false</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>

    <mapping assembly="NHibernateTests"/>
  </session-factory>
</hibernate-configuration>

Which gives me the runtime error : No persister for: NHibernateTests.Classes.Employee

I tried (and error) some setting for mapping element but with no luck. I read how to set ressource for hbm.xml elements but couldn't find an answer for by code mapping.

namespace NHibernateTests.Classes
{
    public class Employee
    {
        public virtual Address Address { get; set; }
        public virtual string FirstName { get; set; }
        public virtual int Id { get; set; }
    }
}


namespace NHibernateTests.Mappings
{
    public class EmployeeMappings : ClassMapping<Employee>
    {
        public EmployeeMappings()
        {
            this.Id(e => e.Id, mapper =>
            {
                mapper.Generator(Generators.HighLow);
            });
        }
    }
}
1

There are 1 best solutions below

0
Radim Köhler On

With a mapping by code you should configure also your factory by code. There is one of few how-to:

NHibernate 3.2 Mapping by Code – Basic Mapping

cited code snippets (see above link for more details)

private static Configuration ConfigureNHibernate()
{
  var configure = new Configuration();
  configure.SessionFactoryName("BuildIt");

  configure.DataBaseIntegration(db =>
  {
    db.Dialect();
    db.Driver();
    db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
    db.IsolationLevel = IsolationLevel.ReadCommitted;

    db.ConnectionStringName = "NH3";
    db.Timeout = 10;

    // enabled for testing
    db.LogFormattedSql = true;
    db.LogSqlInConsole = true;
    db.AutoCommentSql = true;
  });

  var mapping = GetMappings();
  configure.AddDeserializedMapping(mapping, "NHSchemaTest");
  SchemaMetadataUpdater.QuoteTableAndColumns(configure);

  return configure;
}

thew way how to get HbmMapping

private static HbmMapping GetMappings()
{
  var mapper = new ModelMapper();

  mapper.AddMappings(Assembly.GetAssembly(typeof(ProvinceMap)).GetExportedTypes());
  var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

  return mapping;
}