There are three patterns I know of for mapping class hierarchies to tables of a relational database:
- One table for the entire hierarchy. Every row will contain the union of all the attributes of the classes of the hierarchy. This is in my opinion hard to maintain if there are changes to the classes. Every row needs a type identifier, of course.
- One table for every concrete class of the hierarchy. In that case, a table that represents a concrete class contains the attributes of this class plus the ones of all abstract base classes. Abstract classes are not mapped directly.
- One table for every abstract and concrete class. In that case, every single class is directly mapped to a single table.
The advantage of 1. is that it is fairly easy to achieve what you want to do, i.e. read all objects that are part of your class hierarchy. You waste some space, though. In 2., you could read every table in a sub select of one select statements with constant dummy values for the attributes of the other classes, then UNION them together in a single SQL statement. In 3. the query gets a little more complicated: you have to INNER JOIN the concrete class tables with the base-class tables in the subselects. In neither case would you need a FULL OUTER JOIN, though.
Addendum:
Suppose you have an object hierarchy with a Person
class and two subclasses, Student
and Teacher
. In 2., you'd create two tables, STUDENT and TEACHER, with both have a column NAME. The STUDENT table has an additional column YEAR, while the TEACHER table has a column SALARY. Your query might look like this:
SELECT 'STUDENT' AS TYPE, NAME, YEAR, NULL AS SALARY FROM STUDENT
UNION SELECT 'TEACHER' AS TYPE, NAME, NULL AS YEAR, SALARY FROM TEACHER
which you can then retrieve and use to fill your objects with data or display using a SimpleCursorAdapter
or whatever you like.