There are five approaches to map class hierarchies. I will give you a quick overview before the next chapters describes all the details.
Mapped super class
If the parent class is not an entity but only provides some common attributes and methods, then you have to annotate it with @MappedSuperclass. Once it is annotated, the attributes will be stored in the tables of the sub classes LibraryMouse and KitchenMouse.
@MappedSuperclass public class Mouse { ...
You cannot have a relation from a class to a Mouse as it is no entity but you are still able to query for the Mouse class.
session.createQuery("select m from Mouse m").list();
This will sent two queries one for KitchenMouse and one for the LibraryMouse table. Then Hibernate will add both results to the result list.
By the way you can query java.lang.Object as well.
Single table
Package : …inheritance.singletable
// Annotation @Inheritance( strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="mouse_type",discriminatorType= DiscriminatorType.STRING) // XML <subclass name="KitchenMouse" discriminator-value="kitchen">
One table for the class hierarchy. A column identifies the type of a table row. In the picture below it is the type column.
Queries are very simple without any needs for joins or unions. Performance is good: Only one statement needed for inserts and updates. No joins when data is selected All individual attributes of a subclass must allow null values, as other subclasses will not set them.
Joined subclass
// Annotation @Inheritance(strategy=InheritanceType.JOINED) // XML <joined-subclass name="LibraryMouse" table="LibraryMouse">
One table for each class of the hierarchy including parent and subclasses.
Common fields in common table, individual fields in subclass table. Two statements are needed for inserts or updates. Join needed when parent or subclass is selected.
Mixing Joined subclass with a discriminator
XML only!
Package: …inheritance.joineddiscriminator
<subclass name="LibraryMouse" discriminator-value="LibraryMouse"> <join table="LibraryMouse">
One table for each class of the hierarchy including parent and subclasses. Discriminator column to identify object type
Queries are not always easy when relations have to be queried. Common fields in common table, individual fields in subclass table. Two statements needed for inserts and updates. Join needed when parent or subclass is selected but join is faster as compared to joined-subclass.
Table per class
Package: …inheritance.union (XML) and .inheritance.tableperclass (annotation)
// Annotation @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) // XML <union-subclass name="GirlGroup" table="tgirlgroup">
One table for each subclass and optional for the parent class.
A sub class instance is stored in one table. Only one query needed for inserts. Big union including all subclasses when parent is called. No union when subclass is called. Id generator limitations foreign key relations are not possible
XML includes
XML only Package: inheritance.xmlinclude
<class name="OldBoyGroup" table= "toldboygroup"> \&allproperties;
One table for each subclass . XML inclusion is used
Relations to parent class are not easy to query. Only one query needed for inserts. Query to parent class needs one select for each subclass.