Creating class hierarchy mappings
It's common to have an inheritance hierarchy of subclasses. In this example, we will show you one method for mapping inheritance with NHibernate, called table-per-class hierarchy.
Getting ready
Complete the previous Mapping a class with XML example.
How to do it…
- Create a new class named
Book
with the following code:namespace Eg.Core { public class Book : Product { public virtual string ISBN { get; set; } public virtual string Author { get; set; } } }
- Create a new class named
Movie
with the following code:namespace Eg.Core { public class Movie : Product { public virtual string Director { get; set; } } }
- Change the
Product
mapping to match the XML shown in the following code:<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Eg.Core" namespace="Eg.Core"> <class name="Product"> ...