Dot Net developer
Dot Net developer

Reputation: 436

Could not compile the mapping document: NHibernate

I am completely new to NHibernate. I saw many questions with the same title but I am unable to find the exact error. I am using NHibernate with SQL Server 2012.

My hibernate.cfg.xml :

<?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.SqlClientDriver</property>
        <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
        <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=cafePOSdb;Integrated Security=True;</property>
        <property name="show_sql">true</property>
        <mapping assembly="CafePOS" />
    </session-factory>
</hibernate-configuration>

My mapping model:

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

namespace CafePOS
{
    public class CafeTableGroup 
    {
        //properties here
    }
}

My hbm.xml file:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="CafePOS" namespace="CafePOS" xmlns="urn:nhibernate-mapping-2.2">
  <class name="cafe_table_group" table="cafe_table_group" lazy="true" >
   <!--properties here -->
  </class>
</hibernate-mapping>

My SessionFactory class:

namespace XXXXXX
{
    public sealed class SessionFactory
    {
        private static volatile ISessionFactory iSessionFactory;
        private static object syncRoot = new Object();
        public static ISession OpenSession
        {
            get
            {
                if (iSessionFactory == null)
                {
                    lock (syncRoot)
                    {
                        if (iSessionFactory == null)
                        {
                            Configuration configuration = new Configuration();
                         Assembly assembly = Assembly.GetCallingAssembly();
                             configuration.AddAssembly(assembly);
                           iSessionFactory = configuration.BuildSessionFactory();
                        }
                    }
                }
                return iSessionFactory.OpenSession();
            }
        }
    }
}

This is the function that I tried to implement:

public static string Add(CafeTableGroup group)
{
    using (ISession session = SessionFactory.OpenSession)
    {
        using (ITransaction transaction = session.BeginTransaction())
        {
            try
            {
                session.Save(group);
                transaction.Commit();
                return "1";
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                session.Close();
                throw ex.InnerException;
            }
        }
    }
}

I get error on the line

configuration.AddAssembly(assembly);

showing the error in the title:

Could not compile the mapping document: NHibernate

Inner exception message:

Could not find the dialect in the configuration

Thanks in advance.

Upvotes: 1

Views: 349

Answers (2)

HGMamaci
HGMamaci

Reputation: 1387

Your class name is not cafe_table_group, but CafeTableGroup.

Can you try by changing your hbm.xml to

<class name="CafeTableGroup" table="cafe_table_group" lazy="true">
             **************

Upvotes: 1

HGMamaci
HGMamaci

Reputation: 1387

You should have the file hibernate.cfg.xml in the root folder.

Sample content of the file is:

<?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="dialect">Netsis.Framework.Persister.Hibernate.Dialect.NMsSql2008Dialect, Netsis.Framework.Persister</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">Data Source=localhost;Initial Catalog=CRM;Persist Security Info=True;User ID=user;Password=pass</property>
        <property name="proxyfactory.factory_class">Netsis.Framework.Persister.Hibernate.Proxy.PropertyReaderProxyFactoryFactory,Netsis.Framework.Persister</property>
        <property name="show_sql">True</property>
        <property name="format_sql">True</property>
        <property name="adonet.batch_size">30</property>  
    </session-factory>
</hibernate-configuration>

Upvotes: 0

Related Questions