This is the easiest strategy among all. In this strategy, hibernate creates a different table for each subclass and parent class. The disadvantage of this approach is that duplicate columns are created in the subclass table.
Consider a new table structure as shown in the following table:
Update the following code in their respective files:
@Entity @Table(name="employee") @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public class Employee { @Id @GeneratedValue(strategy = GenerationType.TABLE) @Column(name="id") private long id; @Column(name="name") private String name; //getters and setters }
Source file: ContractualEmployee.java
@Entity @AttributeOverrides({ @AttributeOverride(name="id", column = @Column(name="id")), @AttributeOverride(name="name", column = @Column(name="name")) }) public class ContractualEmployee extends Employee { @Column(name="hourly_rate") private Double HourlyRate; @Column(name="contract_period") private Float ContractPeriod; //getters and setters }
Source file: PermanentEmployee.java
@Entity @AttributeOverrides({ @AttributeOverride(name="id", column = @Column(name="id")), @AttributeOverride(name="name", column = @Column(name="name")) }) public class PermanentEmployee extends Employee { @Column(name="salary") private Double salary; //getters and setters }
Use the following table script if the hibernate.hbm2ddl.auto
configuration property is not set to create
:
Use the following script to create the employee
class:
CREATE TABLE `employee` ( `id` bigint(20) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) );
Use the following script to create the contractualemployee
class:
CREATE TABLE `contractualemployee` ( `id` bigint(20) NOT NULL, `name` varchar(255) DEFAULT NULL, `contract_period` float DEFAULT NULL, `hourly_rate` double DEFAULT NULL, PRIMARY KEY (`id`) );
Use the following script to create the permanentemployee
class:
CREATE TABLE `permanentemployee` ( `id` bigint(20) NOT NULL, `name` varchar(255) DEFAULT NULL, `salary` double DEFAULT NULL, PRIMARY KEY (`id`) );
Following are the annotations used in Employee.java
:
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
: This annotation defines the inheritance strategy to be used for an entity class hierarchy. It is used only with the parent or root classes.Following are the annotations used in PermanentEmployee.java
and ContractualEmployee.java
:
@AttributeOverrides({ @AttributeOverride(name="id", column = @Column(name="id")), @AttributeOverride(name="name", column = @Column(name="name")) })
@AttributeOverrides
: This annotation is used to override the mappings of multiple properties or fields@AttributeOverride
: This annotation is used to override the mappings of basic properties or fieldsHibernate creates a column in the table for the attributes that are overridden by the parent class, in which it generally creates redundant data.
As in the other inheritance strategies, we will insert three records here and see how this strategy works. Update the following code:
Session session = sessionFactory.openSession(); Transaction transaction = session.getTransaction(); transaction.begin(); Employee employee = new Employee(); employee.setId(1); employee.setName("Aarush"); session.save(employee); PermanentEmployee permanentEmployee = new PermanentEmployee(); permanentEmployee.setId(2); permanentEmployee.setName("Mike"); permanentEmployee.setSalary(10000D); session.save(permanentEmployee); ContractualEmployee contractualEmployee = new ContractualEmployee(); contractualEmployee.setId(3); contractualEmployee.setName("Vishal"); contractualEmployee.setHourlyRate(200D); contractualEmployee.setContractPeriod(100F); session.save(contractualEmployee); transaction.commit(); session.close();
Hibernate: insert into employee (name, id) values (?, ?) Hibernate: insert into PermanentEmployee (name, salary, id) values (?, ?, ?) Hibernate: insert into ContractualEmployee (name, contract_period, hourly_rate, id) values (?, ?, ?, ?)
The following employee
table shows the database table structure after saving three records:
id |
name |
---|---|
|
|
The following is the database table structure for the contractualemployee
table:
id |
name |
contract_period |
hourly_rate |
---|---|---|---|
|
|
|
|
The following is the database table structure for the permanentemployee
table:
id |
name |
salary |
---|---|---|
|
|
|
Hibernate creates a separate table for all the subclasses. We can see here that an overridden attribute is created in each table.
The disadvantage of this strategy is that if we add, delete, or update a field in the root class, it causes major changes in the subtable as well. This is because in this strategy, the parent class is scattered into the other subclasses and the subclasses use the field of the parent class.