Search This Blog

Monday, August 19, 2013

Inheritance Mapping Stategies in JPA

In JPA, an entity class may inherit from another entity class its behavior and state. The behavior and state become shared between entity classes enabling the inheritance of existing mappings. There are several types of inheritance which are  Joined Strategy, Single-Table Strategy, Table-per-Concrete-Class Strategy. Two of these types; Single table inheritance and Joined table inheritance have many similarities:

When it comes to mapping inheritance, JPA supports three different strategies. When an entity hierarchy exists, it always has an entity as its root. The root entity class can define the inheritance strategy by using the @Inheritance annotation. If it doesn’t, the default single-table-per-class strategy will be applied.

Their object model is the same.
  • They can be configured using either annotations or XML.
  • A discriminator column (a single column used for distinguishing to which class type a database row belongs) is required on the database.
  • The underlying database structure of the single table and joined table inheritance is, however, slightly different.
please refer to each type link for further discussion, example and code snippts.

Joined Strategy

In the joined table inheritance, each class shares data from the root table. In addition, each subclass defines its own table that adds its extended state. The following example shows two child tables, EXTERNAT_VET and IN_HOUSE_VET, as well as parent table VET.
Click here for mode details about Joined Strategy

Single-Table Strategy

In the single table inheritance, the entire class hierarchy is represented by a single table. As the following example shows, the Three classes map to the same VET_ALL table.
Click here for mode details about Single Table Strategy


Table-per-Concrete-Class Strategy

In the table-per-concrete class, each Table contains the shared data as its specific data. In addition, each subclass defines its own table that adds its extended state. The following example shows two child tables, VET_IN and VET_OUT.
Click here for mode details about Table-per-Concrete Class Strategy

No comments: