1 / 25

Inheritance Mapping Patterns

Inheritance Mapping Patterns. Brett Daniel. Object/Relational : Apples/Oranges. Persistent storage is essential for many OO systems. Fundamental mismatch between object-oriented data model and relational data storage . Effect of Inheritance.

etenia
Download Presentation

Inheritance Mapping Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Mapping Patterns Brett Daniel

  2. Object/Relational : Apples/Oranges • Persistent storage is essential for many OO systems. • Fundamental mismatch between object-oriented data model and relational data storage

  3. Effect of Inheritance • Inheritance makes O/R mapping especially difficult.

  4. Inheritance 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

  5. Alternatives • Transaction script • Just run queries • Use a relational domain model • Table Data Gateway • Row Data Gateway • Active Record • Table Module • Ad-hoc

  6. Example Domain Model

  7. Example Inheritance Mapper

  8. Find() 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 Executive’s data then calls load on its superclass. After all loads return, find returns the filled object insert and update act similarly.

  9. EmployeeMapper vs. AbstractEmployeeMapper • AbstractEmployeeMapper loads and saves data for the Employee class. • EmployeeMapper instantiates Employee objects. • Methods delegate to appropriate concrete mapper.

  10. Aside: Generic Inheritance • Why find is not defined in the base class:“Common OO languages can’t 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

  11. Generic Inheritance Example public abstract class MapperBase<DomainObjectType, KeyType> { public void update(DomainObjectType domainObject) { /* ... */ } /* ... */ public abstract DomainObjectType find(KeyType key); /* ... */ } public abstract class AbstractEmployeeMapper<EmployeeType extends Employee> extends MapperBase<EmployeeType, Long> { /* ... */ } public static class HourlyEmployeeMapper extends AbstractEmployeeMapper<HourlyEmployee> { @Override public HourlyEmployee find(Long key) { // ... } }

  12. Inheritance 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

  13. Single Table Inheritance “Represents an inheritance hierarchy of classes as a single table that has columns for all the fields of the various classes.”

  14. Single 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

  15. Single 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

  16. Class Table Inheritance “Represents an inheritance hierarchy of classes with one table for each class.”

  17. Class Table Inheritance • Unique key options • One key per entity • Each table has own primary key, links to superclass table’s foreign key • Retrieval Options • “Leaf” mapper retrieves record containing all fields using join • Each mapper executes query on corresponding table

  18. Class 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

  19. Concrete Table Inheritance “Represents an inheritance hierarchy with one table per concrete class”

  20. Concrete 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

  21. Concrete 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

  22. Choosing 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

  23. Soapbox • 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

  24. Questions?

  25. Resources • 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/essays/mappingObjects.html • Solving the Data Access problem: to O/R map or not To O/R map http://weblogs.asp.net/fbouma/archive/2004/10/09/240225.aspx

More Related