The Inquisitive Coder

Thinking outside of the typical .NET box

NHibernate Mapping Examples

Posted by Davy Brion on July 15, 2007

This blog has been moved to my own site. You can find this post here

16 Responses to “NHibernate Mapping Examples”

  1. Julian Says:

    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.

  2. Davy Brion Says:

    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.

  3. Tobin Harris Says:

    That’s a cool diagram :-)

  4. Jean Says:

    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

  5. Anonymous Says:

    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. ;)

  6. Anonymous Says:

    Question - any particular reason you chose to let NHibernate use direct field access? Is there a performance advantage by doing so? Thanks.

  7. Davy Brion Says:

    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.

  8. wayde Says:

    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

  9. Davy Brion Says:

    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.

  10. Arvind Says:

    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

  11. Davy Brion Says:

    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.

  12. Harsimrat Thukral Says:

    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 ?

  13. Davy Brion Says:

    your mapping files didn’t make it through apparantly so i can’t reallly say much about this…

  14. hamlet Says:

    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?

  15. Davy Brion Says:

    did you make any changes to the file? because it works over here

  16. 2 Links Today (2008-01-16) Says:

    [...] NHibernate Mapping Examples « Davy Brion’s Blog [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>