NHibernate Mapping Examples
Posted by Davy Brion on July 15th, 2007
When you’re starting out with NHibernate, it’s sometimes hard to find good examples. Most examples online are too simple, or are incomplete (just the mappings, but no code for instance). It’s always easier if you have some examples that are large enough, but still small enough to be easy to grasp. So i created mappings and classes for the Northwind tables. I figured this could be useful reference material for anyone new to NHibernate so i’m making the whole thing available for everyone. You’ll find examples of one-to-many, many-to-one and many-to-many associations in there. I used a couple of different cascade options for the associations, depending on the constraints on the tables. I’ve also included about 60 unit tests to verify the mappings are working correctly. These tests should also give you a good idea about how NHibernate deals with certain mappings and options.
I did modify the Northwind database here and there… the zip file contains all the sql create scripts. This is what my version looks like:
And the class diagram looks like this:
I do want to make it clear that this is just an example. I’m not saying that this how you should map your objects and their associations to tables and their relationships. I pretty much provided every possible association in this example, whereas on a real project i’d only create the associations that i actaully need to implement the required functionality. But for the purpose of this example, i thought it would be a good idea to provide as much as possible.
You can download it here


July 16th, 2007 at 12:00 am
Really nice, I just took a look through the mappings in the solution. I just wondered if you could have supplied some examples of mappings using lists and maps as well. One can hardly ever find examples using these types of mappings, while bags and set are often demonstrated.
Keep up the good work,
Julian.
July 16th, 2007 at 12:14 am
Good idea, i’ll try to put a new version of this online in a few days with some examples of lists and maps as well.
There are a lot of other possible mappings that i haven’t explored yet, but if there are any other suggestions, i’ll gladly try to implement them.
July 24th, 2007 at 11:49 pm
That’s a cool diagram
September 5th, 2007 at 12:08 pm
Great job.
The only thing I would like to see is some comments in your code as I am new to ORM tools and C#.
Just a suggestion though!
Cheers
September 10th, 2007 at 4:54 pm
Thanks for the example diagrams and code. Small enough yet very useful as an sample.
One suggestion - you might want to add a copy of nunit.framework to the Lib folder to make the solution more self-contained.
September 10th, 2007 at 4:59 pm
Question - any particular reason you chose to let NHibernate use direct field access? Is there a performance advantage by doing so? Thanks.
September 10th, 2007 at 9:58 pm
thx for pointing out the nunit assembly issue… i’ve updated the zip file so it should be fixed now.
As for the direct field access… that’s just a habit to be honest. I prefer to let NHibernate use the fields directly so i’m free to put whatever code i want in my setters without having to worry about it being executed when NHibernate is initializing an object. Granted, in these examples it’s unnecessary. As for performance, i haven’t benchmarked it but the direct field access should actually be a little bit slower than using properties.
October 3rd, 2007 at 2:19 am
Great set of example mappings.
Do you write up your mapping and class files by hand or use some type of code generation? Personally, I think it would be nice to at minimum be able to generate the mapping files given one or more tables/views in a database from which to work with. Basic stubbing out of a class file based on each mapping file would also be nice to start out with.
If you have any suggestions, I’d be interested to know. Thanks again for your post.
- wg
October 3rd, 2007 at 8:44 am
I did those manually, but at work we do have a custom code generator which will generate the mapping files and the classes based on the database metadata. If you go that route, i would suggest making it possible to differentiate between generating the files initially, and being able to regenerate them constantly. In some cases, you will want to do something different than what was generated by default, so you should be able to modify the generated files without being worried that they will be overwritten the next time you re-generate.
October 6th, 2007 at 6:04 pm
Davy,
Looks cool! What is the custom code generator tool you use at work? I am looking for some tool to generate mapping files and the business entities.
Thanks
October 7th, 2007 at 10:28 am
Well i wrote the custom code generator for the client so unfortunately i can’t share it. Otherwise i would’ve put it online already :). You can also try the templates at http://www.mygenerationsoftware.com allthough i haven’t used MyGeneration yet so i don’t know if it’s any good. But it’s free so it won’t hurt to give it a shot.
October 13th, 2007 at 8:17 pm
I have two tables something like this :
Table : Account
AccountID
UserName
Password
Table : Address
AddressID
AccountID
Line1
Line2
State
My NHibernate mapping file looks like this :
For Table Account
For Table : Address
My Problem is it does entry in the Account table perfectly, but when it comes to do entry in Address Table it says AccountID can’t be null, I’m wondering shouldn’t it pick it up from the Account table as I’m passing in that object.
Is there something in the code need to be fixed ?
October 14th, 2007 at 11:36 am
your mapping files didn’t make it through apparantly so i can’t reallly say much about this…
October 15th, 2007 at 9:59 pm
When I test this solution, I get exeption: “Could not compile the mapping document: Northwind.Domain.Mappings.ProductCategory.hbm.xml”. Why? What can I do?
October 15th, 2007 at 10:12 pm
did you make any changes to the file? because it works over here
January 16th, 2008 at 4:18 pm
[...] NHibernate Mapping Examples « Davy Brion’s Blog [...]
May 18th, 2008 at 2:46 pm
Do you have some data for this new structure? or sql-script to transfer data from old northwind to northwind by u?
May 18th, 2008 at 2:57 pm
unfortunately i don’t…
July 8th, 2008 at 9:54 pm
[...] or Entity Framework or anything. You’ll need to use something else. In my example code I’m using Davy Brion’s NHibernate Northwind Sample as it’s a .NET 2.0 compiled solution and it’ll be acting as my [...]
July 22nd, 2008 at 8:04 pm
[...] I used NHibernate as my Data Layer recently when I got ASP.NET MVC running under .NET 2.0 using NHibernate examples from Davy Brion (who has an NHibernate Category on his [...]