7

Problem Encountered

At runtime, I always get the following NHibernate.MappingException:

"Could not compile the mapping document: GI.InventoryManager.CYB.Mappings.Part.hbm.xml"

Yes, its build action is set to Embedded Resource. The InnerException says:

"Could not find the dialect in the configuration"

Required Information

Here is my configuration file named hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">
        Server=(local);initial catalog=GI_IM_CYB;Integrated Security=SSPI
    </property>
    <property name="adonet.batch_size">10</property>
    <property name="show_sql">false</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="use_outer_join">true</property>
    <property name="command_timeout">60</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,     NHibernate.ByteCode.Castle</property>
  </session-factory>
</hibernate-configuration>

Which actually is a copy-paste from the Configuration_Templates folder in which I only changed the following information:

Session Factory: "Removed the NHibernate.Test namespace and let the property for itself"
Dialect: "From MsSql2000Dialect To MsSql2005Dialect"
Connection_String: "I changed the Initial Catalog attribute to input my own database name"
Factory Class: "From LinFu to Castle"

And here's how I'm using it in my code:

private void configBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) {
    Configuration c = new Configuration();
    c.AddAssembly(typeof(Part).Assembly);
    lock (_sessionFactory) {
        _sessionFactory = c.BuildSessionFactory();
    }
}

Optional Information

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="GI.InventoryManager.CYB" namespace="GI.InventoryManager.CYB.Types">
  <class name="Part" table="Parts" lazy="true">
    <id name="Id" column="part_id">
      <generator class="native"/>
    </id>
    <properties name="Description"/>
    <properties name="Number"/>
    <properties name="InStockQty"/>
    <properties name="Cost"/>
  </class>
</hibernate-mapping>


public class Part {
    #region Private Members

    private string _description;
    private string _number;

    #endregion
    #region Constructors

    /// <summary>
    /// Initializes an instance of the GI.InventoryManager.CYB.Types.Part class.
    /// </summary>
    public Part() { }

    #endregion
    #region Properties

    /// <summary>
    /// Gets or sets the description of this part.
    /// </summary>
    public virtual string Description {
        get {
            return _description;
        } set {
            if (!string.IsNullOrWhiteSpace(value))
                _description = value.Trim();
        }
    }

    /// <summary>
    /// Gets the underlying datastore unique identifier.
    /// </summary>
    public virtual int Id { get; private set; }

    /// <summary>
    /// Gets or sets the user-defined number.
    /// </summary>
    public virtual string Number {
        get {
            return _number;
        } set {
            if (!string.IsNullOrWhiteSpace(value))
                _number = value.Trim();
        }
    }

    /// <summary>
    /// Gets or sets the in-stock quantity.
    /// </summary>
    public virtual int InStockQty { get; set; }

    /// <summary>
    /// Gets or sets the cost.
    /// </summary>
    public virtual double? Cost { get; set; }

    /// <summary>
    /// Gets the inventory value for this part.
    /// </summary>
    /// <remarks>
    /// <para>
    /// This read-only property returns the product of <see cref="T:InStockQty"/> and <see   cref="Cost"/>. 
    /// In case the <b>Cost</b> property does not have a value, zero is returned.
    /// </para>
    /// </remarks>
    public double InventoryValue {
        get {
            if (Cost.HasValue)
                return InStockQty * Cost.Value;
            return 0.0;
        }
    }

    #endregion
    #region Methods



    #endregion
}

Environment

  1. Windows 7 Pro;
  2. Visual Studio 2010, targeting .NET 4.0;
  3. NHibernate 3.0.0.GA;
  4. SQL Server 2005.

Question

I have already tried to put the dialect property on the line of the configuration, and it neither worked.

How to solve this dialect problem that I have?

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
  • 1
    Download the NHibernate source code, attach to the app and try to catch the exception when thrown. – Jahan Zinedine Jan 08 '11 at 10:47
  • haven't worked with NH 3 yet - I'm a bit surprised to read urn:nhibernate-configuration-___2.2____ in your config file. – Marijn Jan 08 '11 at 13:02

2 Answers2

10

Looks allright to me ... have you seen these related questions:

These are easy mistakes to make that can raise the given exception.

Community
  • 1
  • 1
Marijn
  • 10,367
  • 5
  • 59
  • 80
  • +1 I've made sure of the above-mentioned information, but still the error persists. I'll see what I can do with what Jani proposed in his comment. Thanks for your kind assistance! =) – Will Marcouiller Jan 10 '11 at 12:12
  • Hi Marijn! I'm sorry, I had to put this project aside for a while and I can't look forward to it as of now. I shall either accept or tell you what is not working later on when I can come back to this project from which this question is issued. Thanks for your understanding and patience. =) – Will Marcouiller Mar 09 '11 at 16:26
  • This also solved my NHibernate.MappingException: No persister for 'class' exception. – Arafat Nov 13 '18 at 13:00
3

Two things will fix the issue:

Do not use this:

Configuration c = new Configuration();

Instead, use this:

Configuration c = new Configuration().Configure();

Make sure that either you do this in your hibernate.cfg.xml file:

<mapping assembly="Your assembly"/>

OR

AddAssembly(Assembly.GetCallingAssembly());

Doing both will create issue.

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
Rajesh
  • 99
  • 1
  • 1