Barbarian Meets Coding
barbarianmeetscoding

WebDev, UX & a Pinch of Fantasy

1 minute readorm

nhibernate

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

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

Relationships

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>
<set name="Addresses" table="Address">
    <key column="CustomerId"/>
    <many-to-many column="AddressId" class="Address"/>
</set>

<!-- 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"/>

References


Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García