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 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
<!-- Customer --> <one-to-one name="Person"/>
- NHibernate Fundamentals at Pluralsight
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).Follow @vintharas