Smart    Lucky

Smart Lucky

  • NA
  • 555
  • 639.5k

Getting Exception Using Nhibernate

Nov 30 2013 6:15 AM
Hi dudes i am trying to use Nhibernate following way but i am getting exception ("Association refernce unmapped class nhibernate.guitarstore.common.inventory")
can any one help me.....?

-------------------------Guitar.cs-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHibernate.GuitarStore.Common
{
    class Guitar
    {
        public virtual Guid Id { get; set; }
        public virtual string Type { get; set; }
        IList<Inventory> Inventory { get; set; }
    }
}
-------------------------Inventory.cs-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NHibernate.GuitarStore.Common
{
    internal class Inventory
    {
        public Inventory()
        {
        }

        public virtual Guid Id { get; set; }
        public virtual Guid TypeId { get; set; }
        public virtual string Builder { get; set; }
        public virtual string Model { get; set; }
        public virtual int? QOH { get; set; }
        public virtual decimal? Cost { get; set; }
        public virtual decimal? Price { get; set; }
        public virtual DateTime? Received { get; set; }

    }
}






------------------Guitar.hbm.xml---------------------------
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.GuitarStore" namespace="NHibernate.GuitarStore.Common">
<!--<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Stock" namespace="Stock.Models.Classes">-->

    <class name="NHibernate.GuitarStore.Common.Guitar, NHibernate.GuitarStore" table="GUITAR">
    <id name="Id"  column="ID"  type="System.Guid"/>
    <property name="Type"  column="TYPE"  type="System.String"/>
    <bag name="Inventory" table="INVENTORY" lazy="true">
      <key column="TYPEID"/>
      <one-to-many class="NHibernate.GuitarStore.Common.Inventory"/>
    </bag>
  </class>
</hibernate-mapping>

-------------------Inventory.hbm.xml-------------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.GuitarStore" namespace="NHibernate.GuitarStore.Common">
  <class name="NHibernate.GuitarStore.Common.Inventory,NHibernate.GuitarStore" table="INVENTORY">
      <id  name="Id"  column="ID"  type="System.Guid"/>
      <property name="TypeId"  column="TYPEID"  type="System.Guid"/>
      <property name="Builder" column="BUILDER" type="System.String"/>
      <property name="Model"  column="MODEL"  type="System.String"/>
      <property name="QOH"  column="QOH"  type="System.Int32"/>
      <property name="Cost"  column="COST"  type="System.Decimal"/>
      <property name="Price"  column="PRICE"  type="System.Decimal"/>
      <property name="Received" column="RECEIVED" type="System.DateTime"/>
    </class>
</hibernate-mapping>

-------------------------------------NHibernateBase---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using System.Threading.Tasks;

namespace NHibernate.GuitarStore.DataAccess
{
    public class NHibernateBase
    {
        private static Configuration Configuration { get; set; }
        protected static ISessionFactory SessionFactory { get; set; }
        private static ISession session = null;
        private static IStatelessSession statelessSession = null;

        public static Configuration ConfigureNHibernate(string assembly)
        {
            Configuration = new Configuration();
            Configuration.AddAssembly(assembly);
            return Configuration;
        }

        public void Initialize(string assembly )
        {
            Configuration = ConfigureNHibernate(assembly);
            SessionFactory = Configuration.BuildSessionFactory();
        }

        public static ISession Session
        {
            get
            {
                if (session == null)
                {
                    session = SessionFactory.OpenSession();
                }
                return session;
            }
        }

        public static IStatelessSession StatelessSession
        {
            get
            {
                if (statelessSession == null)
                {
                    statelessSession = SessionFactory.OpenStatelessSession();
                }
                return statelessSession;
            }
        }

        public IList<T> ExecuteICriteria<T>()
        {
            using (ITransaction transaction = Session.BeginTransaction())
            {
                try
                {
                    IList<T> result = Session.CreateCriteria(typeof (T)).List<T>();
                    transaction.Commit();
                    return result;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
    }
}

-------------------------------Calling Here--------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Cfg;
using NHibernate.GuitarStore.DataAccess;
namespace NHibernate.GuitarStore.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                NHibernateBase NHB = new NHibernateBase();
                NHB.Initialize("NHibernate.GuitarStore");
                System.Console.WriteLine("NHibernate.GuitarStore assembly initialized.");
                System.Console.ReadLine();
            }
            catch (Exception ex)
            {
                string Message = ex.Message;
                if (ex.InnerException != null)
                {
                    Message +="-InnerExcepetion: " + ex.InnerException.Message;
                }
                System.Console.WriteLine();
                System.Console.WriteLine("*****ERROR*****");
                System.Console.WriteLine(Message);
                System.Console.WriteLine();
                System.Console.ReadLine();

            }
        }
    }
}