Before giving you a detailed introduction to the three mapping methods of the Hibernate inheritance tree, let everyone know that the employee class is an abstract class, and then introduce it comprehensively.
In the domain model, in addition to association and aggregation relationships between classes and classes, there can also be inheritance relationships, a one-to-many two-way association between company class and employee class (assuming that employees are not allowed to work concurrently in multiple companies), and that the employee class is an abstract class, so it cannot be instantiated. It has two specific subclasses: the Hourlyemployee class and the SalariedEmployee class. Because Java allows only one class to have a direct parent class at most, the employee class, the Hourlyemployee class, and the SalariedEmployee class form a tree of inheritance relationships.
In the object-oriented category, there is also the concept of polymorphism, which is based on the inheritance relationship. Simply understood, polymorphism means that when a Java application variable is declared as an employee class, the variable can actually refer either to an instance of the Hourlyemployee class or to an instance of the SalariedEmployee class. The following program code shows polymorphism:
List employees= businessService.findAllEmployees();
Iterator it=employees.iterator();
while(it.hasNext()){
Employee e=(Employee)it.next();
if(e instanceof HourlyEmployee){
System.out.println(e.getName()+" "+((HourlyEmployee)e).getRate());
}else
System.out.println(e.getName()+" "+((SalariedEmployee)e).getSalary());
}
The Findallemployees () method of the Businessservice class retrieves all employee objects from the database through the Hibernate API. The collection returned by the Findallemployees () method contains both an instance of the Hourlyemployee class and an instance of the SalariedEmployee class, which is called a polymorphic query. The variable e in the above program is declared to be an employee type, and it can actually refer either to an instance of the Hourlyemployee class or to an instance of the SalariedEmployee class.
In addition, a polymorphic association is from the company class to the employee class because the employees collection of the company class can contain instances of the Hourlyemployee class and the SalariedEmployee class. From the employee class to the company class is not a polymorphic association, because the employee class's company property refers only to instances of the company class itself. There is no inheritance relationship between database tables, so how do you map the inheritance relationship of the domain model to the relational data model?
Each concrete class of the Hibernate inheritance tree corresponds to a table: the relational data model does not support inheritance relationships and polymorphism in the domain model at all.
The root class of the Hibernate inheritance tree corresponds to a table: unconventional design of relational data models, and addition of fields of additional area molecular types in database tables. In this way, you can enable relational data models to support inheritance relationships and polymorphism.
Hibernate each class of the inheritance tree corresponds to a table: A foreign key reference relationship is used in the relational data model to represent an inheritance relationship.
Hint: The concrete class refers to a class that is not abstract, and the concrete class can be instantiated. The Hourlyemployee class and the SalariedEmployee class are concrete classes.
Each of these mapping methods has pros and cons, and this chapter only describes the three mapping methods for the Hibernate inheritance tree.