Bidirectional one-to-many class relationships

It's often very useful to have a bidirectional relationship between entities. It also simplifies matters for NHibernate, which can often produce more efficient persistence queries when both sides of a relationship are involved.

In this recipe, we will show you how to set up a bidirectional one-to-many relationship between two entity classes.

How to do it…

  1. Add a new folder named Bidirectional to the MappingRecipes project.
  2. Add the following Order class:
    public class Order { private ISet<OrderItem> _items; private ISet<Project> _projects; public virtual Guid Id { get; protected set; } public Order() { _items = new HashSet<OrderItem>(); _projects = new HashSet<Project>(); } public virtual IEnumerable<OrderItem> ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.