Barbarian Meets Coding

WebDev, UX & a Pinch of Fantasy

1 minute readorm


NHibernate is an ORM (object relation mapper) for .NET inspired by Hibernate.

In these code examples I will use the basic xml configuration.


Relationships in NHibernate are very tightly related to relationships in a SQL database.

One to Many Associations

  • One to Many Relationships

** <set><one-to-many class="Order"/></set> ** two tables and an FK CustomerId in Order table

  • Many to One Relationship

** <many-to-one name="Customer"/>

  • Many to many

** two tables and an FK CustomerId in Order table ** <many-to-many/> ** joining table with two FKs, one to each related table

<!-- Customer -->
<set name="Orders" table="`Order`">
    <key column="CustomerId"/>
    <one-to-many class="Order" />
<set name="Addresses" table="Address">
    <key column="CustomerId"/>
    <many-to-many column="AddressId" class="Address"/>

<!-- Order -->
<many-to-one name="Customer" column="CustomerId"/>

One to One Association

Relationship where two tables share the same PK.

<!-- Customer -->
<one-to-one name="Person"/>


Jaime González García

Written by Jaime González García , Dad, Husband, Front-end software engineer, UX designer, amateur pixel artist, tinkerer and master of the arcane arts. You should follow him on Twitter where he shares useful stuff! (and is funny too).Jaime González García