A polymorphic query is one were we can query on the Parent class and as a result get instances of
Parent,
LeftChild and
RightChild. If we set up the descriptor to support polymorphic queries then the following query will return instances of all
Parent,
LeftChild and
RightChild objects stored in the database.
In this method each concrete class in the class hierarchy has its own table. That table holds all the instance variables for the class. For an example of this method we will make the
Parent class an abstract class. So we need two tables, which we will call LEFTCHILDTABLE and RIGHTCHILDTABLE. The columns in each table are illustrated below.
The typeResolverForParent method returns the actual resolver, which is used by GLORP to determine which tables to examine for polymorphic queries. With the above setup the following query will return all
Parent,
LeftChild and
RightChild objects with left as the value of the instance variable top. Note that for the descriptors for
LeftChild and
RightChild the
typeResolverFor: registers the parent class (
Parent), not the
LeftChild or
RightChild. This is what permits the polymorphic query.
In this method of organizing the tables there is one table that holds the data for all objects in the class hierarchy. In this example the one table is called ALL_DATA. The columns of the table are illustrated below. The object_type column is used to store the type of the object in the row. This allows GLORP to create an instance of the correct class when it reads a row in the table. Note that regardless of what type of object is in a row, one column, either left_value or right_value, will be null. While this method does waste space in the database, performing a polymorphic query will be faster as there is only one table to access.
Even though Parent is still an abstract class, GLORP requires us to give the mappings for the
Parent instance variables. If
Parent were not abstract we would have to provide a key value for the column object_type.
In the above examples the resolver type of the class Parent was defined in a method in the
GlorpTutorialDescriptor. If you prefer you can define in the
Parent class itself. So you could delete the GlorpTutorialDescriptor>>typeResolverForParent method and add the following class method to Parent