NHibernate version: NHibernate 2.0 with VS 2005
Mapping documents:
Table name and column names are for sure ok.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model">
<class name="Model.Products, Model" table="Products">
<id name="ProductId" column="ProductID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="ProductName" column="ProductName" type="string" length="40" not-null="true" />
<property name="SupplierId" column="SupplierID" type="int" not-null="false" />
<property name="CategoryId" column="CategoryID" type="int" not-null="false" />
<property name="QuantityPerUnit" column="QuantityPerUnit" type="string" length="20" not-null="false" />
<property name="Discontinued" column="Discontinued" type="bool" not-null="false" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
Configuration configuration = new Configuration(); //1
//configuration.AddAssembly(typeof(Model.Products).Assembly); //2
//configuration.AddFile("Products.hbm.xml"); //3
//configuration.AddClass(typeof(Model.Products)); //4
configuration.Configure();
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
And class:
Code:
namespace Model
{
public class Products
{
private int _productId;
private string _productName;
private int _supplierId;
private int _categoryId;
private string _quantityPerUnit;
private bool _discontinued;
public int ProductId
{
get { return _productId; }
set { _productId = value; }
}
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
public int SupplierId
{
get { return _supplierId; }
set { _supplierId = value; }
}
public int CategoryId
{
get { return _categoryId; }
set { _categoryId = value; }
}
public string QuantityPerUnit
{
get { return _quantityPerUnit; }
set { _quantityPerUnit = value; }
}
public bool Discontinued
{
get { return _discontinued; }
set { _discontinued = value; }
}
}
}
Full stack trace of any exception that occurs:In attepts number: 1, 2 I have the following stack trace:
NHibernate.MappingException was unhandled
Message="No persister for: Model.Products"
Source="NHibernate"
StackTrace:
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName, Boolean throwIfNotFound)
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
at NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
at NHibernate.Impl.SessionImpl.Get[T](Object id)
at DataAccessLayer.ProductsRepository.GetById(Int32 ProductId) in E:\projekty\NHibernate\NHibernateNorthWind\DataAccessLayer\Repositories\ProductsRepository.cs:line 19
at NHibernateTest.Program.Main(String[] args) in E:\projekty\NHibernate\NHibernateNorthWind\NHibernateTest\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
In attept 3 and 4 I recived file not found exception and after I added mapping file to .../bin folder i get "Could not compile the mapping document.." error:
NHibernate.MappingException was unhandled
Message="Could not compile the mapping document: Products.hbm.xml"
Source="NHibernate"
StackTrace:
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
at NHibernate.Cfg.Configuration.AddXmlFile(String xmlFile)
at NHibernate.Cfg.Configuration.AddFile(String xmlFile)
at DataAccessLayer.NHibernateManager.get_SessionFactory() in E:\projekty\NHibernate\NHibernateNorthWind\DataAccessLayer\NHibernateManager.cs:line 22
at DataAccessLayer.NHibernateManager.OpenSession() in E:\projekty\NHibernate\NHibernateNorthWind\DataAccessLayer\NHibernateManager.cs:line 39
at DataAccessLayer.ProductsRepository.GetById(Int32 ProductId) in E:\projekty\NHibernate\NHibernateNorthWind\DataAccessLayer\Repositories\ProductsRepository.cs:line 17
at NHibernateTest.Program.Main(String[] args) in E:\projekty\NHibernate\NHibernateNorthWind\NHibernateTest\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Name and version of the database you are using: MSSQL2005I've created class library project in my solution to separate bussines entites as assembly named Model. I've added reference to Model assembly in DataAccessLayer assembly and NHibernateTest console application (+ DataAccessLayer refference). I have no idea what could be wrong in this mapping file. I was doing it step by step by example showed in here
http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspxThe code that calls ISessionFactory.OpenSession:
Code:
using Model;
namespace DataAccessLayer
{
public static class ProductsRepository
{
public static Products GetById(int ProductId)
{
Products retObj = null;
using (ISession session = NHibernateManager.OpenSession())
{
retObj = (Products)session.Get<Products>(ProductId);
}
return retObj;
}
}
}
I've tried to solve this problem by myself for hours and I've runned out of all ideas :) Could one help me?
PS: I rebuild Model assembly after changing mapping files, so out of date assembly is not the case :)