Check out my "Real" NHibernate Example App

As most of you know that follow my blog, I not only have my own WilsonORMapper, but I've also been tinkering with NHibernate some.  Why?  I needed to use a "free" set of code for my recent presentation on O/R Mapping at the Atlanta Code Camp.  Anyhow, here's the long promised link to download my "real" NHibernate example app.  What do I mean by calling it "real"?  Well to put it bluntly, my experience is that its impossible to find a realistic and decently complex app for NHibernate (or Hibernate).  My example app is the exact same one I updated for my WilsonORMapper, except I modified it slightly to support NHibernate.  This means that it is first of all a real and fully functioning WinForm app -- complete with data-binding in grids and all your basic CRUD.  It also means that this example includes several types of primary keys, relationships, and inheritance -- and did I mention it works!

Sure there is lots of documentation for Hibernate (and thus for NHibernate too), including books, but the examples are either typically small snippets, and many are outdated and just don't work with NHibernate v0.8.  Some people have criticized the small amount of documentation I have for my WilsonORMapper, and rightly so, but I've always included a real example app that goes a long way in getting people started -- and without this I found it quite difficult to get up to speed on NHibernate since I was forced to dig into scattered documents, forums, and just plain old-fashioned trial-and-error in many cases!  Here's an example -- NHibernate has multiple ways to model related collections (sets, bags, lists, and maps), which certainly is an advantage if you need flexibility, but just try to figure out which one to use for a many-to-many related collection that you want to be bindable -- the answer is use the bag.

Anyhow, what's intriguing to me is how close both the basic approach and the mapping file syntax is for my WilsonORMapper and NHibernate -- they are remarkably similar and there's very little effort to switch between them.  Of course there are certainly differences too -- NHibernate is more flexible if you need that (most don't), while my WilsonORMapper is a little easier (far simpler codebase), supports more databases (including Access), and provides DAL capabilities as well as O/R Mapping.  Both mappers have features the other lacks -- but they both have the basic set of features that most people need -- and both work as my example apps demonstrate.  What are some other features my WilsonORMapper has?  Supports stored procedures, custom collection types, real batch updates/deletes, server-side paging, lazy-loading without keeping "session" open, interface to avoid reflection, null values even in .NET v1.1, and I'm sure there are others I could list.

What about NHibernate you ask?  I can't do it much justice to be fair since I'm not that experienced with it, but a few come to mind: more caching options, normalized inheritance, built-in one-to-one, two-way graph syncs, more cascade options, eager load option -- and again there are bound to be more.  But the basics are pretty similar (although I prefer my WilsonORMapper of course) -- so if you're an NHibernate fan, then please download and enjoy my "real" NHiberate example app -- and feel free to add your own comments here on either mapper (or others), although please include some real details if you do.

Published Thursday, May 26, 2005 10:43 AM by PaulWilson


# re: Check out my "Real" NHibernate Example App

Hi Paul,

Thanks for the example, i will check it out.

btw. what a links, trying to get google to like you? :-)

Thursday, May 26, 2005 1:55 PM by Joe

# re: Check out my "Real" NHibernate Example App

Martijn's Cuyahoga is probably the only other real example I've seen, but to me it was too much of a real app and not enough of an example -- not any fault of his, afterall that's the point since it is a real app that he's making with it.

As for refactoring into different layers -- I don't think its a challenge at all actually. Its mostly the same principles you would apply in any other layerd app, although easier in my opinion, but of course I left that undone in my example.

Thursday, May 26, 2005 2:47 PM by Paul Wilson

# re: Check out my "Real" NHibernate Example App

I was able to make NHibernate work against access using the oledb option.

Saturday, May 28, 2005 3:28 PM by Udi Dahan - The Software Simplist

# re: Check out my "Real" NHibernate Example App

Hi Paul,

Thanks for the example app - I've been looking for something like this for a couple of weeks now. This one looks like it demonstrates a lot of the main features I would like to take advantage of.

However, I'm having trouble getting the example app to run. In the LoadContacts() method, when I hit this line:
this.ContactGrid.DataSource = session.CreateQuery(query).List();

I get this error:

An unhandled exception of type 'NHibernate.ADOException' occurred in nhibernate.dll

Additional information: Could not execute query.

Any suggestions on what I might be doing wrong?


Saturday, May 28, 2005 9:34 PM by Dale Smith

# re: Check out my "Real" NHibernate Example App

Hey Dale:

Hopefully someone more experienced with NHibernate can shed some light on it. The only thought I have is probably not the issue, but I suppose I should ask anyhow -- have you created the database with the sql script? Yea, probably not the issue. :)

Good luck, Paul

Saturday, May 28, 2005 10:27 PM by Paul Wilson

# re: Check out my "Real" NHibernate Example App

Yeah, I ran the script to create the db instead of doing it by hand. I haven't been able to get ISession.CreateQuery() to work in either your example or my practice app. Still can't figure it out. If I come up with the solution, I'll post it here.

Saturday, May 28, 2005 10:46 PM by Dale Smith

# re: Check out my "Real" NHibernate Example App

Hi Paul,

I figured it out. Dumb move on my part: for some reason the ORMExample login didn't work for me. I just changed it to a login that does work and voila, I'm in business!

Now if I can figure why I'm messing around with computers at midnight on a Saturday night instead of messing around with my wife, I'll be making some real progress.


Sunday, May 29, 2005 1:14 AM by Dale Smith

# re: Check out my "Real" NHibernate Example App


There is probably something wrong with your connection string. Take a look in the log.txt file for more details about the exception.

Sunday, May 29, 2005 8:46 AM by Martijn Boland

# re: Check out my "Real" NHibernate Example App

This is one of the few examples available about NHibernate, that actually represent something out of the real world.
But there's one mayor thing I don't like about the code. It is impossible to reuse the business model in some other form of presentation layer.
That is one of THE things you need when building extendable and maintainable applications.

Friday, June 3, 2005 10:17 AM by WillemM

# re: Check out my "Real" NHibernate Example App

Its definitely not intended to be a model for best practices. I actually thought about making it a little better in that regard, but I think it would make it harder to grasp the NHibernate usage for some that aren't as experienced.

Friday, June 3, 2005 10:30 AM by Paul Wilson

# re: Check out my "Real" NHibernate Example App

Hi Corbin:

Most O/R Mappers (EntityBroker being the exception) assume that you will provide the plumbing for distributed apps, whether it be web services or remoting, just like you would in any other case -- and its not that much different, so I don't really see the problem.

Wednesday, June 15, 2005 2:23 PM by Paul Wilson

# is anyone using this sample app for sql 2005?

If so, were you able to use the appConfig connection section "as is"?
I am experiencing connection probs :-(
If somemone can confirm that the conenction should work as is, then that'd be a help.


Friday, June 17, 2005 4:54 AM by yogi

# re: Check out my "Real" NHibernate Example App

It should just be a simple matter of getting the connection string correct -- especially if you have a named instance for Sql 2005.

Friday, June 17, 2005 7:02 AM by Paul Wilson

# re: Check out my "Real" NHibernate Example App

at least I know where I am going wrong.I have tried integrated security etc in the connString.Still no joy.

Could someone who is using the sample app with sql2005 email / post their nHibernate dbConn section?I apologise for this slightly off-topic :-(


Friday, June 17, 2005 10:38 AM by yogi

# re: Check out my "Real" NHibernate Example App

Thanks for the 'real' example Paul.
Was beginning to doubt whether I'd find anything resembling a real-world scenario for nHibernate on the web.

Sunday, June 19, 2005 8:25 AM by Simon Stewart

# re: Check out my "Real" NHibernate Example App

Thanks for the nice example.
But can you tell me, from where can i get the documentation on NHibernate.

Wednesday, June 22, 2005 8:35 AM by Wilson

# re: Check out my "Real" NHibernate Example App

Uh, my guess would be the NHibernate site.

Wednesday, June 22, 2005 9:26 AM by Paul Wilson

# re: Check out my "Real" NHibernate Example App

This is the first NHibernate (or Hibernate) example that I was able to run successfully.  I ran it using VS.NET 2005 using MSDE for the database.  The only change I made was to update App.config with the database instance I configured for this example (as opposed to using the default instance).  The example is sufficiently rich in features to make it a good basis for further study in understanding the Hibernate configuration and programming details.  Thank you very much for taking the time to post this example.

Saturday, June 3, 2006 7:38 PM by Al

# re: Check out my "Real" NHibernate Example App


Tuesday, June 13, 2006 11:25 AM by MOBI

# re: Check out my "Real" NHibernate Example App

You shouldn't need to do anything differently for Sql Server 2005, other than change your connection string if that's necessary on your install.  As for hbm.xml errors, I would think that you would get more help on the NHibernate forums, from regular NHibernate users, instead of asking me.  I'm pleased to offer something to the NHibernate community, but keep in mind that I do have my own ORMapper that occupies my time.

Tuesday, June 13, 2006 1:52 PM by PaulWilson

# re: Check out my "Real" NHibernate Example App

Hi, Paul Thanks for your work, it's very interesting and much more completed than other. I would ask a question. In the example you use always session.CreateQuery so you use the IQuery interface. So you must make the query string (SQL string). In other example I have seen session.CreateCriteria. In the first you must know the DB structure and think in Relational mode (SELECT, JOIN, OUTER JOIN and so on). In the second you think in more OO. It's true? It is better use session.CreateCriteria? I'm novice in NHibernate but I think that I must masquerade the DBMS. A real application with NHibernate don't must have SQL-spagetti (also for security reason aka SQL Ingnition)

Thursday, June 29, 2006 12:38 PM by Antonio

# re: Check out my "Real" NHibernate Example App

First, keep in mind that I'm not an NHibernate expert since I have my own ORMapper -- so I simply used what I did to make it work and don't claim its "best".  But having said that, I actually do like to think in Relational mode and don't view that as a problem -- but I agree its nice that an OO viewpoint is also supported.  I do the same thing with my ORMapper -- I tend to build Sql strings, since that's my viewpoint, but the ORMapper also supports OPath for OO thinking.  For my ORMapper though I do have a QueryHelper that helps me build those Sql strings, so I don't have to hard-code table and filed names, and I can also make sure my Sql strings are "clean" to avoid Sql injection.  I don't know if NHibernate has a similar thing to avoid such hard-coding and Sql injection, so while that's not a problem with my ORMapper, it may be more advisable with NHibernate to avoid it.

Thanks, Paul Wilson

Friday, June 30, 2006 5:42 AM by PaulWilson

# re: Check out my "Real" NHibernate Example App

man, seems in connection string shall change "User=ORMExample" to "Uid=ORMExample"

Thursday, July 20, 2006 11:10 AM by athos

# re: Check out my "Real" NHibernate Example App

I have been searching for an example of NHibernate and was glad to find this. I'm kind of a newbie, so sorry but how do you get the database data? Thanks.

Wednesday, November 8, 2006 12:46 AM by GO

# re: Check out my "Real" NHibernate Example App

I have been looking for a good example for long time now - i found yours and it were working the first time - damn good work! :)

Sunday, May 13, 2007 3:39 PM by Bjarne Pedersen