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.
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.
相关文章推荐
- Mapping persistent classes(Chapter 3 of Hibernate In Action)
- Hello NHibernate(Chapter 2 of NHibernate in action)
- Introducing and integrating Hibernate(Chapter 2 of Hibernate In Action)
- Chapter 3 A top-level view of computer function and interconnection
- Think Python - Chapter 17 - Classes and methods
- Pasteboards and Nil-Targeted Actions(Chapter 21 of Cocoa Programming for Mac OS X)
- Synchronization and Time(Chapter 2 of Concurrent Programming on Windows)
- Working with persistent objects(Chapter 4 of Hibernate In Action)
- Spring and POJO-based remote services(Chapter 8 of Spring In Action)
- Types and Declarations(Chapter 4 of The C++ Programming Language)
- Classes(Chapter 10 of The C++ Programming Language)
- Chapter 4– Functions and Program Structure of TCPL (Part 10)
- Inversion of Control/Dependency Injection with Repository Pattern, Fluent Nhibernate, and LightCore
- Working with Characters and Strings(Chapter 2 of Windows Via C/C++)
- Working with Characters and Strings(Chapter 3 of Windows Via C/C++)
- Think Python - Chapter 15 - Classes and objects
- Angel Borja博士教你如何撰写科学论文三:Writing the first draft of your science paper — some dos and don’ts
- chapter 2 : The Complexity of Algorithms and the Lower Bounds of Problems
- Drag-and-Drop(Chapter 23 of Cocoa Programming for Mac OS X)