Ray's playground

 

Writing and Mapping classes(Chapter 3 of NHibernate In Action)

Mapping class inheritance

A simple strategy for mapping classes to database tables might be “one table for every class.” This approach
sounds simple, and it works well until you encounter inheritance.
Inheritance is the most visible feature of the structural mismatch between the object-oriented and relational
worlds. Object-oriented systems model both “is a” and “has a” relationships. SQL-based models provide only
“has a” relationships between entities.
There are three different approaches to representing an inheritance hierarchy. These were catalogued by
Scott Ambler [Ambler 2002] in his widely read paper “Mapping Objects to Relational Databases”:
􀂃 Table per concrete class—Discard polymorphism and inheritance relationships completely from the
relational model
􀂃 Table per class hierarchy—Enable polymorphism by denormalizing the relational model and using a
type discriminator column to hold type information
􀂃 Table per subclass—Represent “is a” (inheritance) relationships as “has a” (foreign key) relationships
This section takes a top down approach; it assumes that we’re starting with a domain model and trying to
derive a new SQL schema. However, the mapping strategies described are just as relevant if we’re working
bottom up, starting with existing database tables.

posted on 2009-11-05 19:38  Ray Z  阅读(211)  评论(0编辑  收藏  举报

导航