Maddy
Maddy

Reputation: 2224

Inheritance Mapping in JPA

I was going through the JPA specification on available strategies to map an inheritance relationship to the relational model. I didn't quiet understand the following statements in the spec:

The Single Table per Class Hierarchy Strategy:

"This strategy provides good support for polymorphic relationships between entities and queries that cover the entire entity class hierarchy"

The Table per Concrete Class Strategy:

"This strategy provides poor support for polymorphic relationships"

Can anyone further clarify the meaning of these statements on how polymorphism is affected by these strategies?

Upvotes: 2

Views: 1305

Answers (4)

kucing_terbang
kucing_terbang

Reputation: 5131

Single Table per Class Hierarchy

this strategy provides a good polymorphic relationships between entity and query is because that JPA can predict what entity is the query for by its DiscriminatorValue. example, let say you have an entity like this.

select a, b, c, type from table_c;

and by using JPA, you can get class A or class B even though you're using table_c and it also easier for you to create a new entity by just adding a new column into that table. (this is what people usually mean by Denormalisation)

Table per Concrete Class

this strategy provides a poor support for polymorphic relationships is because one table is only for one class. which mean that if you need to create a new entity, then you should create a new table.

Upvotes: 0

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

Consider we have a domain model as below enter image description here

Please study the links provided below.

Single Table Strategy:

enter image description here

  • All data is stored in single table
  • Hence complex joins are not required when retrieving or inserting data
  • Polymorphic means multiple entities, in single table, those all are stored in one table. Hence no need for joins
  • But this wastes database space as columns need to be nullable single table example

Joined Table Strategy enter image description here

  • Entity data is stored in separate tables
  • Joins are required to insert and retrieve entity data
  • Insertion and retrieval becomes heavy because joins are needed
  • Saves database space because columns need not be nullable

Joined table example

Table per concrete class enter image description here - Again, data is stored in multiple tables hence

Table per concrete class exmaple

It all depends on your requirement. If you have ample database and need performance, Single table strategy is the way to go. If you have database constraint, then other strategies are to be followed.

Upvotes: 4

Pratik
Pratik

Reputation: 954

Conside below example.The Table per Concrete Class Strategy

enter image description here

Here each concrete class is mapped as normal persistent class. so we have 3 tables; PERSON, EMPLOYEE and OWNER to persist the class data. In this the mapping of the subclass repeats the properties of the parent class.

Here you have few disadvantages :

  1. if you make any Changes to a parent class is reflected to large number of tables
  2. A query couched in terms of parent class is likely to cause a large number of select operations
  3. Data thats belongs to a parent class is scattered across a number of subclass tables, which represents concrete classes.

Upvotes: 2

Neil Stockton
Neil Stockton

Reputation: 11531

See this page, which has examples of each http://www.datanucleus.org/products/accessplatform_4_1/jpa/orm/inheritance.html

Upvotes: 0

Related Questions