Mapping Table per Concrete Class

Hibernate Table per concrete class strategy

You can use the Table per concrete class strategy in two ways:

1) Table per concrete class with implicit polymorphism

The table contains all the properties of the concrete class and the properties that are inherited from its superclasses. All subclasses are mapped as separate entities.

Example:

package com.sample
public class Vehicle {
    // Constructors and Getter/Setter methods,
    long id;
    int noOfTyres;
    private String colour;

}
package com.sample
public class Car extends Vehicle {
    // Constructors and Getter/Setter methods,
    private String licensePlate;
    long price;
    int speed;

}

2) Table per concrete class with unions

Using table per concrete class with unions, the superclass can be an abstract class or even an interface. The limitation of this approach is that if a property is mapped on the superclass, the column name must be the same on all subclass tables.

Example:

package com.sample;

@Entity 
@Inheritance (strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Vehicle {
 @Id 
 @GeneratedValue (strategy=GenerationType.AUTO) 
 @Column  
 long id;
 int noOfTyres;
 private String colour;

}
package com.sample;

@Entity
 public class Car extends Vehicle {
    // Constructors and Getter/Setter methods

 @Column
 private String licensePlate;

 @Column
 long price;

 @Column
 int speed;

}