Object Persistence Layer is a common part of Domain Model and the basis of any successfull application project based on complex Object Oriented Model. This way a lot of commercial and free of use packages are present on the market in turn to help us to reduce the efforts required during Persistence Layer building.
OK. Hibernate is well known package in this area from JAVA world and NHibernate is the stable, free of charge port for .NET Framework.
Well, you are a new NHibernate user which tried to set up the NHibernate and run your first test project based on the NHibernate examples, finally been faced up to the Unknown Entity Class error which could be found at the right part of our page.
This article just trying to help to such users with the problem which seems to be common.
Scopes of the problem : ASP.NET 2.0, MS Visual Studio 2005, NHibernate 1.0.5, Unknown Entity Class NHibernate Mapping Exception.
|
|
Location details :
- The problem : The .xml file might be compiled as embedded resource only for the library projects. This way the NHibernate's AddAssembly method for Configuration can't find the mapping information for Model classes. This way we could suggest to use the
AddXmlFile method instead.
- App_Code is the common ASP.NET 2.0 folder for Visual Studio 2005 and this is the name of assembly which holds the classes included.
- App_GlobalResources is the common ASP.NET 2.0 folder for Visual Studio 2005 which holds and share the resources for application. This way the ClassName.hbm.xml mapping file for NHibernate could be located here. Plus, the name of mapping file might to reflect the Class Namespace hierarhy.
You may found the solution which is not the production environment and reflects the personal opinion of the Author. All copyrights and trademarks are the property of their owners.
|
Required sources and configuration details:
Web.config:
<configSections>
<section name="nhibernate"
type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</configSection>
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.connection.connection_string" value="Server=___;initial catalog=___;Integrated Security=SSPI"/>
</nhibernate>
App_Code/Model/t_Areas.cs:
using System;
namespace Company.Model
{
public class t_Areas
{
private Int64 m_AreaID;
Skipped... Usual class details
App_GlobalResources/Company.Model.t_Areas.hbm.xml Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Company.Model.t_Areas, App_Code" table="_Areas">
<id name="_AreaID" type="Int64" column="AreaID" unsaved-value="0">
<generator class="identity" />
</id>
<property name="_AreaName" column="AreaName" type="String" length="50"/>
<property name="_ImageID" column="ImageID" type="Int64" />
</class>
</hibernate-mapping>
NHibernate helper class ( from documentation ) loads the Mapping xml file from disk:
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
Configuration cfg = new Configuration();
cfg.AddXmlFile(
"G:\\work\\WEB\\Company\\SRC\\App_GlobalResources\\Company.Model.t_Areas.hbm.xml");
sessionFactory = cfg.BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
NHibernate use case:
NHibernate.ISession session = NHibernateHelper.GetCurrentSession();
NHibernate.ITransaction tx = session.BeginTransaction();
t_Areas na = new t_Areas();
na._AreaName = "area name";
na._ImageID = 123;
session.Save(na);
tx.Commit();
NHibernateHelper.CloseSession();
|
|
Known problem :
Server Error in '/SRC' Application.
Unknown entity class: Company.Model.t_Areas
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: NHibernate.MappingException: Unknown entity class: Company.Model.t_Areas
Source Error:
Line 38: na._ImageID = 123;
Line 39:
Line 40: session.Save(na);
Line 41: tx.Commit();
|
| |
|
Last comment:
|
|
The right way to solve the "Unknown entity class" problem for NHibernate is to provide the right architecture solution for the project and to separate the Data Access Layer into the separate DLL (Assembly) project. This way you'll be able to compile the xbm.xml mapping files like embedded resources and to use AddAssembly method for NHibernate's Configuration.
|
|