user2284767
user2284767

Reputation: 3

NHibernate - Could not compile the mapping document

It is my firs time using NHibernate, I'm getting source code for a program from my friend after that, the program is running well after that I'm trying to add "Stock.hbm.xml" as following:

`

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="NBooks.Core.Models"
                   assembly="NBooks.Core">

  <class name="Stock" table="Stocks" lazy="false">

    <id name="ID">
      <column name="Stock_ID" />
      <generator class="identity" />
    </id>

    <property name="Stock_name" column="Stock_name" />
   <property name="Comp_ID" column="Comp_ID" /> 
    <property name="Stock_Code" column="Stock_Code" />
    <property name="Address" column="Address" />
    <property name="Nots" column="Nots" />

  </class>

</hibernate-mapping>

`

with my class "Stock.cs"

 using System;
 using System.Collections.Generic;


 namespace NBooks.Core.Models
 {


public class Stock : BaseModel<Stock> 
{

         public virtual string Stock_name { get; set; }
         public virtual string Stock_Code { get; set; }
         public virtual int Comp_ID { get; set; }
         public virtual string Notes { get; set; }
         public virtual string Address { get; set; }
         public virtual bool Inactive { get; set; }

        public Stock()
         {
         }

         public Stock(string name)
         {
             this.Stock_name = name;
         }


 }


    public class StockEventArgs : EventArgs
    {

        public Stock Stock { get; set; }

        public StockEventArgs(Stock Stock)
        {
            this.Stock = Stock;
        }
 }

    public delegate void StockEventHandler(Stock sender, EventArgs e);

   }

the Base model is:

using System;
using System.Collections.Generic;
using NBooks.Core.Util;
using NBooks.Data.NHibernate;
using NHibernate;

namespace NBooks.Core.Models
{
public interface IBaseModel
{
    int Id { get; set; }
}

public class BaseModel<T> : IBaseModel
{
    IList<string> errors = new List<string>();
    public virtual int Id { get; set; }

    public virtual bool HasErrors {
        get { return errors.Count > 0; }
    }

    public virtual IList<string> Errors {
        get { return errors; }
    }

    public BaseModel()
    {
    }

    public virtual void Validate()
    {
        Errors.Clear();
    }

    public virtual void SaveOrUpdate()
    {
        ITransaction trans = null;
        try {
            ISession session = NHibernateHelper.OpenSession();
            trans = session.BeginTransaction();
            session.SaveOrUpdate(this);
            session.Flush();
            trans.Commit();
        } catch (Exception ex) {
            LoggingService.Error(ex.Message);
            MessageService.ShowError(ex.Message);
            trans.Rollback();
        }
    }

    public virtual void Delete()
    {
        ITransaction trans = null;
        try {
            ISession session = NHibernateHelper.OpenSession();
            trans = session.BeginTransaction();
            session.Delete(this);
            session.Flush();
            trans.Commit();
        } catch (Exception ex) {
            LoggingService.Error(ex.Message);
            MessageService.ShowError(ex.Message);
            trans.Rollback();
        }
    }

    public static T Read(int id)
    {
        return NHibernateHelper.OpenSession().Load<T>(id);
    }

    public static IList<T> FindAll()
    {
        return     
NHibernateHelper.OpenSession().CreateCriteria(typeof(T)).List<T>();
    }
}
}

when build it appers every thing is well and no errors , when run the error "NHibernate - Could not compile the mapping document Stock.hbm.xml" appears. Thanx in advance

Upvotes: 0

Views: 6938

Answers (2)

jaredmahan
jaredmahan

Reputation: 86

I noticed you have a typo in you XML:

<property name="Nots" column="Nots" />

I would suggest that you look into using Fluent NHibernate as well. It is strongly typed (for the most part) and the mapping files are easier to read and use lambda expressions so that you don't have to go the XML route.

Your mapping would instead look like this:

public class StockMap(): ClassMap<Stock>
{
    public StockMap(){  
        Id(x => x.Id).Column("Stock_ID").GeneratedBy.Identity();
        Map(x => x.Comp_ID);
        Map(x => x.Address);
        Map(x => x.Notes);
    }
}

Upvotes: 1

mickfold
mickfold

Reputation: 2003

You have a typo in your mapping

<property name="Nots" column="Nots" />

should be

<property name="Notes" column="Nots" />

Upvotes: 1

Related Questions