您的位置:首页 > 移动开发

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

2009-11-05 19:38 375 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: