Working with the table per concrete class strategy of inheritance

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.

Getting ready

Consider a new table structure as shown in the following table:

Getting ready

Creating the classes

Update the following code in their respective files:

Source file: Employee.java

@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
}

Creating the tables

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`)
);

Annotations used in Employee.java

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.

Annotations used in PermanentEmployee.java and ContractualEmployee.java

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 fields

Hibernate creates a column in the table for the attributes that are overridden by the parent class, in which it generally creates redundant data.

Note

Note that this strategy does not support the IDENTITY and AUTO generator strategies; we have to use other generation strategy options or provide a primary key explicitly.

How to do it…

As in the other inheritance strategies, we will insert three records here and see how this strategy works. Update the following code:

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();

Output

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

1

Aarush

The following is the database table structure for the contractualemployee table:

id

name

contract_period

hourly_rate

3

Vishal

100

200

The following is the database table structure for the permanentemployee table:

id

name

salary

2

Mike

10000

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.231.194