I have a class Contact (base class),a class called Customer and a class called Supplier. Customer and supplier class both derive from Contact.
Customer has a 0..n relat
Another solution if you don't like to use the set from Iesi collections
public class Customer : Contact
{
public ICollection<Order> Orders
{
get; private set;
}
}
And the mapping like this:
<bag name="Orders" table="Customer_Orders" >
<key column="Customer_FK" />
<composite-element>
<property name="OrderNumber" />
<property name="OrderName" />
<!-- ... -->
</composite-element>
</set>
This is done like this:
Create your class like this:
public class Customer : Contact
{
private ISet<Order> _orders = new HashedSet<Order>();
public Collection<Order> Orders
{
return new List<Order>(_orders);
}
// NOrmally I would return a ReadOnlyCollection<T> instead of a Collection<T>
// since I want to avoid that users add Orders directly to the collection.
// If your relationship is bi-directional, then you have to set the other
// end of the association as well, in order to hide this for the programmer
// I always create add & remove methods (see below)
public void AddOrder( Order o )
{
if( o != null && _orders.Contains(o) == false )
{
o.Customer = this;
_orders.Add(o);
}
}
}
in your mapping, you specify this:
<set name="Orders" table="OrdersTable" access="field.camelcase-underscore" inverse="true">
<key column="..." />
<one-to-many class="Order" .. />
</set>
Since you use inheritance, you should definitly have a look at the different posibilities regarding inheritance-mapping in NHibernate, and choose the strategy that is best suited for your situation: inheritance mapping
Regarding set & bag semantics: - when you map a collection as a set, you can be sure that all the entities in the mapped collection, are unique. That is, NHibernate will make sure that, while reconstituting an instance, the collection will not contain duplicates. - when you map the collection as a bag, it is possible that your collection will contain the same entity more then once when loading the object from the DB.