views:

126

answers:

2

Question: I have an annoying problem with nhibernate.
The problem is I cannot get any example I find on the web to work...
I've now tried for two days...


The first problem was it wouldn't read the config file, so I had to move it into app.config / web.config.


The second problem is that whatever I do, I always get an error: No persister for: NHibernate.Examples.QuickStart.User
I've searched google and I did change the mapping to embeded ressource, and I did add the mapping to the config file, but nothing helps...

The example is from this page: https://www.hibernate.org/362.html

I've uploaded my Visual Studio 2005 project to
http://daniel-steiger.ch/exchange/NhibernateCrap.rar

Does anybody know what I do wrong ?

A: 

Clean up your namespace and assembly names and it will probably work. Please don't ask others to find your bug when your bug exists because the code is a mess.

Paco
In that case: Personally I started with NHibernate with the video tutorials of summerofnhibernate.com. At the time, FluentNhiberante did not exist. Now I would use the unit tests of the FluentNhibernate sourcecode as examples for the mapping.
Paco
Well, it's not my code, it's the examples of other people. I've started making changes, and that did correct some to many errors, but still I couldn't get even one example to work... With all the time that took me (but without any useful result), I'd probably be better of writing my own wrapper.
Quandary
+2  A: 

Here's everything I had to fix to get your code working:

  • Configuration.LoadMappingDocument only validates the mapping, but does not add it to the configuration, i.e. it works as documented. It should probably have a better name (like ValidateMapping or something). Use AddFile, AddDocument or similar instead, though I recommend using AddAssembly and having the mapping embedded as a resource;
  • In the mapping when you write <class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples" the part after the comma is the assembly name. It should be <class name="NHibernate.Examples.QuickStart.User, NHibernateCrap", or you should change the assembly name (I'd recommend that);
  • I had to change the mapping XML namespace to the 2.2 one: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  • Finally, because NHibernate lazy-loads by default, you have to make all properties virtual or disable lazy loading (not recommended). The example is actually wrong in this aspect;

For further learning I recommend you read Gabriel Schenker's NHibernate tutorials.

Martinho Fernandes
ah, suspected the mapping file, but didn't know what was wrong.Thank you !
Quandary