Title: Inheritance Mapping Patterns
1Inheritance Mapping Patterns
2Object/Relational Apples/Oranges
- Persistent storage is essential for many OO
systems. - Fundamental mismatch between object-oriented data
model and relational data storage
OODM RDBMS
Class Object Entity State Behavior Inheritance Table Row Key Data Transactions Data Relationships
3Effect of Inheritance
- Inheritance makes O/R mapping especially
difficult.
OODM RDBMS
Class Object Entity State Behavior Inheritance Table Row Key Data Transactions Data Relationships
4Inheritance Mappers
- Each domain class in an inheritance
hierarchy has a mapper that saves and loads the
data for that domain class. - Allows both concrete and abstract classes to
handle their own O/R mapping - Domain objects loosely coupled to database
5Alternatives
- Transaction script
- Just run queries
- Use a relational domain model
- Table Data Gateway
- Row Data Gateway
- Active Record
- Table Module
- Ad-hoc
6Example Domain Model
7Example Inheritance Mapper
8Find() Behavior
- User calls find on concrete mapper object.
- find instantiates a new Executive and passes it
and the database record to load. - load fills in the Executives data then calls
load on its superclass. - After all loads return, find returns the filled
object - insert and update act similarly.
9EmployeeMapper vs. AbstractEmployeeMapper
- AbstractEmployeeMapper loads and saves data for
the Employee class. - EmployeeMapper instantiates Employee objects.
- Methods delegate to appropriate concrete mapper.
10Aside Generic Inheritance
- Why find is not defined in the base
classCommon OO languages cant let you change
the declared return type of a method - One can define find using generic inheritance
- Forces concrete mappers to implement it
- Other methods become type-safe
- Hierarchy becomes more complex
11Generic Inheritance Example
- public abstract class MapperBaseltDomainObjectType,
KeyTypegt - public void update(DomainObjectType
domainObject) / ... / - / ... /
- public abstract DomainObjectType find(KeyType
key) - / ... /
-
- public abstract class AbstractEmployeeMapperltEmplo
yeeType extends Employeegt - extends MapperBaseltEmployeeType, Longgt
- / ... /
-
-
- public static class HourlyEmployeeMapper
- extends AbstractEmployeeMapperltHourlyEmployeegt
- _at_Override
- public HourlyEmployee find(Long key)
- // ...
-
12Inheritance Mapping Schemes
- Inheritance mapping determines how the mappers
load and save domain objects - How is the inheritance hierarchy represented in
the database? - Single Table Inheritance
- Class Table Inheritance
- Concrete Table Inheritance
13Single Table Inheritance
- Represents an inheritance hierarchy of classes
as a single table that has columns for all the
fields of the various classes.
14Single Table Inheritance
- The type field refers to the type of Employee the
record represents - Unused fields are left blank
- Mapper retrieves record, checks type,
instantiates appropriate object, and passes
record to load
15Single Table Inheritance
- Advantages
- Only one table
- No joins when retrieving data
- Fields can move up and down the hierarchy
- Disadvantages
- Not all fields are relevant for all classes
- Wasted space
- Large, confusing table
- Possible field name conflicts
16Class Table Inheritance
- Represents an inheritance hierarchy of classes
with one table for each class.
17Class Table Inheritance
- Unique key options
- One key per entity
- Each table has own primary key, links to
superclass tables foreign key - Retrieval Options
- Leaf mapper retrieves record containing all
fields using join - Each mapper executes query on corresponding table
18Class Table Inheritance
- Advantages
- Easy to understand
- Very clear relationship between class and table
- All columns used high normalization
- Disadvantages
- Multiple tables requires join or multiple queries
- Database changes required when fields move up and
down hierarchy - Supertype table accessed very often
- Must understand the class hierarchy to understand
tables
19Concrete Table Inheritance
- Represents an inheritance hierarchy with one
table per concrete class
20Concrete Table Inheritance
- Can be thought of as a single record for each
entity in the system - Primary keys must be unique across all tables
- Cannot use auto_increment primary key
- Cannot use database to enforce referential
integrity - Retrieval
- Concrete class retrieves record from
corresponding table - Abstract class must query all tables to find
where key exists
21Concrete Table Inheritance
- Advantages
- Tables are independent
- No unused fields within a table
- No joins required
- No single heavily-used table
- Disadvantages
- Difficult to ensure primary key uniqueness and
referential integrity - Database does not contain anything corresponding
to abstract classes - Field modifications require many changes to
database - Abstract class mapper must query multiple tables
to determine what concrete mapper to delegate to
22Choosing a Mapping Scheme
- Size (depth, width) of the hierarchy
- Amount and type of expected changes
- Whether the database is shared with other
applications - Speed requirements
- Type of RDBMS
- Schemes can be combined as needed
23Soapbox
- Personal choice Class table inheritance with
shared primary key and multiple queries - Mappers need only know their own table structure
- No passing around data rows only unique
identifiers - Easy to modify changes to a class prompt very
obvious changes to mapper and table - No complex joins or name conflicts
- Inheritance hierarchy enforced by database using
table keys
24Questions?
25Resources
- Patterns of Enterprise Application Architecture.
Martin Fowler 2003. Pages 278-304. - Mapping Objects to Relational Databases O/R
Mapping In Detail. http//www.agiledata.org/essay
s/mappingObjects.html - Solving the Data Access problem to O/R map or
not To O/R map http//weblogs.asp.net/fbouma/archi
ve/2004/10/09/240225.aspx