This document discusses different approaches for mapping object-oriented class hierarchies and properties to a relational database schema. It describes mapping an entire class hierarchy to a single table, mapping each concrete class to its own table, mapping each class to its own table, and using a generic table structure. It also discusses strategies for mapping class-scope properties that apply to all instances of a class, including using single-column single-row tables, multi-column single-row tables, multi-column single-row tables for all classes, and a multi-row generic schema.
Convert to study materialsBETA
Transform any presentation into ready-made study material!select from outputs like summaries, definitions, and practice questions.
2. Overview (mapping) Most modern business application development projects use object technology such as Java to build the application software and relational databases to store the data. However, there is an impedance mismatch between object and relational technology, because relational databases can hold only scalar values, whereas objects are composite variables. To overcome this impedance mismatch, mapping objects to relational databases is a great solution.
3. Intent (mapping) The heart of the problem is translating the logical representation of the objects into an atomized form that is capable of being stored on the database, while somehow preserving the properties of the objects and their relationships so that they can be reloaded as an object when needed. If this storage and retrieval functionality is implemented, the objects are then said to be?persistent.
4. Mapping Inheritance Structures Relational databases do not natively support inheritance, forcing you to map the inheritance structures within your object schema to your data schema. Approaches The concept of inheritance throws in several interesting twists when saving objects into a relational DB. The ways you can organize the inherited attributes within your data model are: Map the entire class hierarchy to a single table Map each concrete class to its own table Map each class to its own table Map the classes into a generic table structure
5. Problem - first we have a three classes C Person hierarchy - the second version of the hierarchy adds a fourth concrete class The idea is that you have implemented the first class hierarchy and are now presented with a new requirement to support giving executives, but not non-executive employees, fixed annual bonuses.
6. Approach 1 Map the entire class hierarchy to a single table Solution -The first column is the primary key for the table and the second is a code indicating whether the person is a customer, an employee, or perhaps both. -PersonPOID is a persistent object identifier (POID). -The PersonType column is required to identify the type of object that can be instantiated from a given row. For example the value of E would indicate the person is an employee. -When you discover that combinations are possible you should consider applying the Replace Type Code With Booleans database refactoring:
8. Approach 3 Map Each Class To Its Own Table The data for the Customer class for example is stored in two tables, Customer and Person , therefore to retrieve this data you would need to join the two tables (or do two separate reads, one to each table). The addition of views is also an option in many cases. Person_view: Simple query: SELECT name, bonus SELECT name, bonus FROM table_person p JOIN table_executive e FROM Person_view ON p.personPOID = e.personPOID
9. Approach 4 Map Classes To A Generic Table Structure A fourth option for mapping inheritance structures into a relational database is to take a generic, sometimes called meta-data driven approach, to mapping your classes. The value of a single attribute is stored in the Value table, therefore to store an object with ten business attributes there would be ten records, one for each attribute. The Value.ObjectPOID column stores the unique identifier for the specific object (this approach assumes a common key strategy across all objects, when this isn¨t the case you¨ll need to extend this table appropriately). The AttributeType table contains rows for basic data types such as data, string, money, integer and so on. This information is required to convert the value of the object attribute into the varchar stored in Value.Value .
10. Mapping the inheritance structure between Person and Customer into the same schema Each class would be represented by a row in the Class table. There would also be a row in the Inheritance table, the value of Inheritance.SuperClassPOID would refer to the row in Class representing Person and Inheritance.SubClassPOID would refer to the row in Class representing Customer
11. Mapping Class-Scope Properties Sometimes a class will implement a property that is applicable to all of its instances and not just single instances.
12. Strategy Example Advantages Disadvantages Single Column, Single-RowTable The Customer Number table implements this strategy. Simple Fast access Could result in many small tables Multi-Column, Single-Row Table for a Single Class If Customer implemented a second class scope attribute then a CustomerValues table could be introduced with one column for each Attribute Simple Fast access Could result in many small tables, although fewer than the single column approach Multi-Column, Single Raw Table for all Classes The topmost version of the Class Variables table. This table contains one column for each class attribute within your application, so if the Employee class had a nextEmployeeNumber class attribute then there would be a column for this as well. Minimal number of tables introduced to your data schema. Potential for concurrency problems if many classes need to access the data at once. One solution is to introduce a ClassConstants table, as shown in Figure 19, to separate attributes that are read only from those that can be updated. Multi-Row Generic Schema for all Classes The bottommost version of the ClassVariables and ClassConstants . The table contains one row for each class scope property in your system. Minimal number of tables introduced to your data schema. Reduces concurrenyproblems (assuming your database supports row-based locking). Need to convert between types (e.g. CustomerNumber is an integer but is stored as character data). The data schema is coupled to the names of your classes and their class scope properties.