Exercise – Inheritance Mapping in Hibernate Inheritance and polymorphism are powerful features of Java's object-oriented architecture, enabling you to create hierarchies of classes and use one class (base or parent class) as the foundation for another class (derived or child class.) However, mapping a Java hierarchy to a set of relational database tables can be a tricky process. Hibernate offers three techniques to simplify the mapping process -- store each class in a separate table, store all classes in the same table, or store common properties in the same table and move the derived properties to a separate table (joined subclasses.) You will explore these different inheritance mapping schemes in this exercise. Joined Subclasses Part One: Understanding the Schema Our application has a base class called Product, with name, description and price fields. Each product has a supplier, represented by an instance of the Supplier class. There are many possible suppliers for a product, indicated by a many-to-one mapping within Product.hbm.xml. The Product and Supplier tables have a common SUPPLIER_ID column, linking products and suppliers. The Product class is extended to create a child class Software by adding a derived version property. This version uses the "joined-table" approach to implement inheritance -- all of the 'base class' properties are stored in the PRODUCTS table and the 'derived' class properties are stored in the SOFTWARE table. This is reflected in the <joined-subclass> element in the Software.hbm.xml mapping file. The ID column is used to help "stitch" together a Software instance from the appropriate tables. Since a given Supplier may supply many Products, the Supplier class contains a private List of products. This is reflected in the Supplier.hbm.xml mapping by the <bag> mapping (a bag is simply an unordered set) for products, followed by the one-to-many element indicating that a single Supplier is associated (through the SUPPLIER_ID column) with one or more instances of the Product class. Part Two: Understanding the Application The main() routine creates two suppliers, three instances of the class Product and two instances of the class Software (which, you will recall, is a derived class of Product.) After creating the objects, there should be five Products -- three instances of the base class and two instances of the derived class. The application closes and flushes the session, then verifies the created objects by issuing select commands against all three tables. Part Three: Running The Application Unzip the LAB07A.ZIP file and create a new Java project in Eclipse from the supplied build.xml file. Review the Main.java file, then run it. What results do you get? Are the Software entities successfully created as children of Product? Entire Hierarchy In A Single Table Part Four: Understanding the Schema Our base class of compact discs (CDs) has two derived classes -- "International" CDs which may appear in different languages and "Special Edition" CDs with additional features. All CDs have ID, TITLE, ARTIST and PURCHASEDATE fields. The InternationalCDs add a LANGUAGE field and the SpecialEditionCDs add a NEWFEATURES field. This is reflected in a single CD.hbm.xml file for the entire hierarchy. All three classes appear in the file. Each class is mapped to the same table: <class name="CD" table="cd"> <class name="SpecialEditionCD" table="cd"> <class name="InternationalCD" table="cd"> Common properties are repeated in each class mapping. Part Five: Understanding the Application The main() routine creates three CDs -- one of each type -- and saves each CD to the database. The application flushes and closes the persistence context, and then verifies the inheritance hierarchy by asking for all CDs regardless of type. Part Six: Running the Application Unzip the LAB07B.ZIP file and create a new Java project in Eclipse from the supplied build.xml file. Be sure your database server is running. Review the Main.java file, then run it. How many different CDs of each type are created? How many CDs are displayed at the end? Each Class In A Separate Table Part Seven: Understanding the Schema and Strategy The separate-table-per-class strategy works best if you will frequently query specific classes at different levels in the hierarchy, as opposed to making truly polymorphic queries that return a mixture of base-class and derived-class objects. You create the same mapping file for the entire hierarchy as you did in the one-table-per-hierarchy example above, but you map each class to a different table: <class name="CD" table="cd"> <class name="SpecialEditionCD" table="secd"> <class name="InternationalCD" table="icd"> Part Eight: Understanding the Application The main() routine creates three CDs -- one of each type -- and saves each CD to the database. The application flushes and closes the persistence context, and then verifies the inheritance hierarchy by asking for all CDs regardless of type. Part Nine: Running the Application Unzip the LAB07C.ZIP file and create a new Java project in Eclipse from the supplied build.xml file. Be sure your database server is running. Review the Main.java file, then run it. How many different CDs of each type are created? How many CDs are displayed at the end?