Nhibernate vb.net mvc failed to compile mapping
I am new to NhIbernate and Vb.net MVC, I am trying to do this tutorial. I am using VS 2013
http://www.dotnetjalps.com/2013/09/asp-net-mvc-nhibernate-crud-getting-started.html
But using Vb.net instead of C # My current config is such that
I am getting an exception like this:
{"Could not compile the mapping document: d:\documenti\visual studio 2013\Projects\NhibernateMVC\NhibernateMVC\Models\Nhibernate\Employee.hbm.xml"}
{"persistent class NhibernateMVC.Models.Employee, NhibernateMVC not found"}
{"Impossibile caricare il tipo 'NhibernateMVC.Models.Employee' dall'assembly 'NhibernateMVC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.":"NhibernateMVC.Models.Employee"}
This is my Employee.vb class
Namespace NhibernateMVC.Models
Public Class Employee
Public _Id As Integer = 0
Public _FirstName As String = ""
Public _LastName As String = ""
Public _Designation As String = ""
Public Overridable Property Id() As Integer
Get
Return _Id
End Get
Set(ByVal Value As Integer)
_Id = Value
End Set
End Property
Public Overridable Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal Value As String)
_FirstName = Value
End Set
End Property
Public Overridable Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal Value As String)
_LastName = Value
End Set
End Property
Public Overridable Property Designation() As String
Get
Return _Designation
End Get
Set(ByVal Value As String)
_Designation = Value
End Set
End Property
End Class
End Namespace
This is my Employee.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="NhibernateMVC" namespace="NhibernateMVC.Models">
<class name="Employee" table="Employee" dynamic-update="true" >
<cache usage="read-write"/>
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="FirstName" />
<property name="LastName" />
<property name="Designation" />
</class>
</hibernate-mapping>
And this one, NHIbernateSession.vb
Imports System.Web
Imports NHibernate
Imports NHibernate.Cfg
Namespace NhibernateMVC.Models
Public Class NHibertnateSession
Public Shared Function OpenSession() As ISession
Dim configuration As New Configuration()
Dim configurationPath = HttpContext.Current.Server.MapPath("~\Models\Nhibernate\hibernate.cfg.xml")
Dim employeeConfigurationFile = HttpContext.Current.Server.MapPath("~\Models\Nhibernate\Employee.hbm.xml")
configuration.Configure(configurationPath)
configuration.AddFile(employeeConfigurationFile)
Dim sessionFactory = configuration.BuildSessionFactory()
Return sessionFactory.OpenSession()
End Function
End Class
End Namespace
VB.NET has the concept of root namespaces. Basically, the root namespace is added to any namespaces that you declare with Namespace
.
What does it mean, in your class Employee
, when you define a namespace:
Namespace NhibernateMVC.Models
Because of the root namespace of the project (probably NhibernateMVC
if you haven't changed it), the fully qualified class name is actually NhibernateMVC.NhibernateMVC.Models.Employee
.
There are several things you can do to fix this problem:
- Remove the root namespace from the project properties.
-
Change the class namespace
Employee
:Namespace Models
-
Modify the Employee.hbm.xml file to provide the correct fully qualified name:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="NhibernateMVC" namespace="NhibernateMVC.NhibernateMVC.Models">
I would personally go with # 2 as others are confusing. After making your changes, you will face another issue - NHibernate doesn't like that you have public backing fields for your properties. You must configure the class Employee
to use fields Private
for backups:
Namespace Models
Public Class Employee
Private _Id As Integer = 0
Private _FirstName As String = ""
Private _LastName As String = ""
Private _Designation As String = ""
' ... etc
Note: the class NHibertnateSession
has a typo in its name (this is also present in the tutorial). It probably should be NHibernateSession
.