NHibernate Notes1_Creating Class Hierarchy mappings

There are 3 ways to implement the hierarchy in NHibernate: Subclass in one table, Table per class &

Table per concrete class

  1. Subclass in one table

All entities of subclasses are stored in the same table.

Father class

<class name="Product">

<id name="Id">

<generator class="guid.comb" />

</id>

<discriminator column="ProductType" />

<natural-id mutable="true">

<property name="Name" not-null="true" />

</natural-id>

<property name="Description" />

<property name="UnitPrice" not-null="true" />

</class>

Sub class

<subclass name="Movie" extends="Product">

<property name="Director" />

</subclass>

 

Notes:

With table-per-class-hierarchy, we cannot define any of our subclass properties as

not-null="true", because this would create a not-null constraint on those fields

 

  1. Table per class

In table-per-class mappings, properties of the base class (Product) are stored

in a shared table, while each subclass gets its own table for the subclass properties

NHibernate will use a join to query forthis data

 

subclass

<joined-subclass name="Movie" extends="Product">

<key column="Id" />

<property name="Director" />

</joined-subclass>

 

  1. Table per concrete class

In table-per-concrete-class mappings, each class gets its own table containing columns

for all properties of the class and the base class

To fetch Product data, NHibernate will use unions to query athree tables.

 

<union-subclass name="Movie" extends="Product">

<property name="Director" />

</union-subclass>

原文地址:https://www.cnblogs.com/haokaibo/p/2166824.html