Installing NHibernate in your application can be challenging. Trying to keep up with new versions and finding information and examples for that particular version is sometimes difficult. Most of the issues I encountered were due to mashing different versions and hoping that all of the parts will work together. While trying to install NHibernate 2.0.1.GA in an Asp.Net MVC 1.0 web application I found the following errors and their fixes.
1) Using the wrong namespace. You might be following an old example from a previous version using:
hibernate-configuration xmlns=”urn:nhibernate-mapping-2.2″
And maybe getting the following exception:
NHibernate.Cfg.HibernateConfigException was unhandled by user code
Message=”<session-factory xmlns=’urn:nhibernate-configuration-2.2′> element was not found in the configuration file.”
Source=”NHibernate”
StackTrace:
To solve this just replaced your configuration with the new namespace :
<hibernate-configuration xmlns=”urn:nhibernate-configuration-2.2″>
<session-factory>
…
</session-factory>
</hibernate-configuration>
2) MappingException :
NHibernate.MappingException was unhandled by user code
Message=”Unknown entity class: MyDomain.EntityClassName”
For me, NHibernate was unable to find the particular hbm.xml files, so after placing the files in a better location I also make sure to explicitly give the location to the config file:
<hibernate-configuration xmlns=”urn:nhibernate-configuration-2.2″>
<session-factory>
<property name=”dialect”>…</property>
<property name=”connection.provider”>…</property>
<property name=”connection.driver_class”>…</property>
<property name=”connection.connection_string”>…</property>
<mapping assembly=”MyDomain.Mvc.Data”/>
</session-factory>
</hibernate-configuration>
3) Exception: The following types may not be used as proxies: MyDomain.MyClass: method set_MyProperty should be virtual.
Solution: NHibernate requires the properties to be “virtual”.
4) Exception:
Message=”The type initializer for ‘NHibernate.Proxy.Poco.Castle.CastleProxyFactory’ threw an exception.”
Source=”NHibernate”
TypeName=”NHibernate.Proxy.Poco.Castle.CastleProxyFactory”
Solution: Add the “Castle.DynamicProxy2.dll” from the NHibernate installation folder as a reference.
vijay
Point (4) is not cleat, can you please elaborate…
i am getting same error as you have mentioned for point (4).
DaCoder
In #4, the error was caused because my project was missing a reference to “Castle.DynamicProxy2.dll”, which NHibernate needs desperately to be able to build the proxies. In my case, it was an easy fix. In Windows Explorer, I created a \LIB folder in my project, and copy and paste the “Castle.DynamicProxy2.dll” from the NHibernate installation project (the one you download and unzip), and from Visual Studio > View All > Added the \Lib folder and the DLL. Right click the project > Add Reference > Added the DLL from my Lib folder. Additionally, I made sure that the DLL was being COPY during the build process (from the dll’s Properties window in VS). Hope that helps Vijay, and thanks for your comments and views.
vijay
I have added new hbm and mapping class in different folders.
before adding above the code was running fine with existing hbm and mapping classes
DaCoder
Why did you add more HDM and mapping classes? New table perhaps?. It just broke recently after that, make sure your mapping is correct. If you are not getting any specific messages as to why is breaking up, try activating NHibernate logging from the config file and hopefully you can get a more deep insight into what is going on. If it was working before, then concentrate only on the code you added. Is a pain to setup sometimes, but once done and is running, NHibernate benefits are huge. Good luck and thanks for posting again Vijay.