Mapping entities with NHibernate

MappingsThe next thing to be done was NHibernate mapping. Mappings are defined in Peniko.DataLayer project.

Every entity class has its own mapping file. Mapping file is named after a class, so if class name is Category, the mapping file is Category.hbm.xml. hbm stands for Hibernate mapping. Having this naming scheme is not required, but NHibernate will try to look for these names automatically when mapping. If you want to name your mappings differently, then you'll need to tell that to NHibernate.

Mapping file must have Embedded Resource build action. That's the thing that gets easily forgotten, so double check all your mapping files.

Writing a mapping file can be tedious and error-prone, but there is a cure for that. NHibernate comes with two XML schema files (nhibernate-configuration.xsd and nhibernate-mapping.xsd) that you can copy to Program Files\Microsoft Visual Studio 9.0\Xml\Schemas. Once there, they'll provide you with Intelli-sense when writing NHibernate mappings and configuration in Visual Studio.

If you are ReSharper user, there is also a nice ReSharper NHibernate plugin. It checks type, assembly and property names and allows navigation to mapped classes with Ctrl+Click on class or property name. Also, any renames are reflected in mapping file.

Below is an example of a very simple mapping:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

                  assembly="Peniko.Domain"

                  namespace="BLDotNet.Peniko.Domain">

  <class name="Category" table="Categories">

    <id name="Id" column="CategoryId">

      <generator class="guid" />

    </id>

 

    <property name="Name" column="Name" length="50" not-null="true" unique="true" />

  </class>

</hibernate-mapping>

You must fully qualify type names in mapping files ("Namespace.Class, Assembly"), or you can define assembly and namespace attributes on root tag.

If the table name differs from the class name, there has to be a table attribute on <class> tag. In our case, all table names are plural.

id must be defined for all mapped classes. There are different id generators in NHibernate, but you'll most likely choose between "guid" and "native". When "native" generator is selected, NHibernate will use database generated value - this is an identity field in SQL Server. NHibernate maintains ids itself, so you don't have to ever assign values to them (unless you are using "assigned" generator).

All simple properties are mapped with <property> tag. There are also <many-to-one>, <one-to-one> and <one-to-many> tags for relations with other mapped classes; <subclass> and <joined-subclass> for inheritance mapping; <map>, <set>, <list> and <bag> for collection mapping and even more tags.

This should be enough to get you started with NHibernate mapping. For more details see NHibernate learning material.


6 comments:

  1. Nikola Malovic

    You might want to check fluent nhibernate project

    Here's wiki page for fluent mapping

    http://wiki.fluentnhibernate.org/Fluent_mapping

     
  2. Miroslav Popovic

    You didn't check the date of this post. It's over one year old :)

    I'm already using Fluent NHibernate on two projects.

     
  3. Nikola Malovic

    AutoMap or FluentMap?

    I found FluentMap only usable in brownfield-real-world apps, but I wonder what your experiences are

     
  4. Miroslav Popovic

    Both projects I'm using Fluent NHibernate on are greenfield applications, but we started with Fluent Mapping since it was more discoverable in learning process.
    During the development, more and more things were pushed to conventions, so now we have rather plain ClassMaps and it wouldn't require much effort to switch entirely to Auto Mapping.
    I guess that next touching of mapping code would be an ideal time to make a switch :)

     
  5. Nikola Malovic

    Would those be some OSS or comercial projects? If OSS, it would be really nice comparing databse, current fluent mappings and auto mappings in same source

    I'm also working on one comercial app currentlly using FNH (while considering EF4 option based UoW just now) so I can not share my example :(

     
  6. Miroslav Popovic

    Unfortunately, both projects are commercial.

    Bobby Johnson has started pretty interesting screencast about Fluent NHibernate: Vacation of Fluent NHibernate. Looking forward to his today's post about Auto Mapping.

     

Post a Comment