NHibernate Notes1_Creating Class Hierarchy mappings
2011-09-04 22:50 小郝(Kaibo Hao) 阅读(254) 评论(0) 编辑 收藏 举报There are 3 ways to implement the hierarchy in NHibernate: Subclass in one table, Table per class &
Table per concrete class
- 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
- 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>
- 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>
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。