Article directory
introduction
Hibernate is an ORM (Object Relational Mapping) framework based on the Java language. It provides a variety of ways to create tables through entity inheritance. This article will introduce three commonly used entity inheritance strategies in Hibernate, and explain in detail the table structure design and entity class mapping configuration process of each strategy.
1. Overview of Hibernate entity inheritance
1.1 The concept and role of inheritance
Entity inheritance is a common concept in object-oriented programming that allows the creation of new entity classes by extending existing entity classes. In Hibernate, entity inheritance is mainly used to handle the inheritance relationship between objects, and at the same time, it can be effectively mapped to the database table structure.
1.2 Entity inheritance method in Hibernate
Hibernate provides three main entity inheritance strategies: single table inheritance strategy, concrete class inheritance strategy, and mapping superclass strategy. In the following, we will introduce the specific implementation methods of these strategies one by one.
1.3 Basic annotations
@Inheritance
and @DiscriminatorColumn
are JPA annotations, used to specify the inheritance relationship and distinguishing columns of entity classes.
-
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
:@Inheritance
Annotation is used to specify the inheritance strategy of entity classes. In the example, the specified strategy is single-table inheritance (InheritanceType.SINGLE_TABLE
), which means that all entity classes involved in the inheritance relationship will be stored in the same database table. -
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
:@DiscriminatorColumn
Annotation is used to specify the name and type of the distinguishing column. In the example, the specified distinguishing column isdiscriminator
a string type column named (DiscriminatorType.STRING
). The purpose of a distinguishing column is to distinguish different entity types based on their values so that correct object instantiation can be performed at query time.
Taken together, when using the single-table inheritance strategy, @Inheritance
annotations are used to declare the inheritance relationship, and @DiscriminatorColumn
the names and types of distinguishing columns are specified through annotations. In the generated database table, a distinguishing column will be included to store the identification value of each entity type so that it can be correctly restored to the corresponding object type when querying the data.
2. Single table inheritance strategy
2.1 Overview
The single-table inheritance strategy maps all related entity classes into one database table by adding a discriminator
column to distinguish different entity types.
2.2 Table structure design
2.3 Entity class mapping configuration
Use annotations in each entity class @DiscriminatorValue
to specify the value corresponding to the entity type discriminator
.
@Entity
@Data
@DiscriminatorColumn(name = "discriminator", discriminatorType=DiscriminatorType.STRING)
public abstract class Animal {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
private boolean sex;
}
@Entity
@Data
@DiscriminatorValue("B")
public class Bird extends Animal {
private int height;
}
@Entity
@Data
@DiscriminatorValue("P")
public class Pig extends Animal {
private int weight;
}
3. Concrete class inheritance strategy
3.1 Overview
The concrete class inheritance strategy maps each concrete entity class to a separate database table, each with its own primary key.
3.2 Table structure design
Create a database table corresponding to each entity class, each table containing a separate primary key column.
3.3 Entity class mapping configuration
注意:这种策略的实体生成表主键不可以自动生成,这也是为什么下面的代码种主键生成策略的注解被注释的原因
@Entity
@Data
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Animal {
@Id
//@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
private boolean sex;
}
@Entity
@Data
public class Bird extends Animal {
private int height;
}
@Entity
@Data
public class Pig extends Animal {
private int weight;
}
4. Mapping super class strategy (one table for each class)
4.1 Overview
The mapped superclass strategy places common attributes in a superclass and maps the superclass to an independent database table, with each subclass mapping only its own unique attributes.
4.2 Table structure design
Create a database table that contains the properties of the superclass, and then create a separate database table for each subclass.
4.3 Entity class mapping configuration
Use annotations on the super class @Inheritance(strategy = InheritanceType.JOINED)
, and use annotations on the subclass @Table
to specify the corresponding database table name. I didn't use it here @Table
because the table name is created according to the class name by default.
@Data
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Animal {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
private boolean sex;
}
@Entity
@Data
public class Bird extends Animal {
private int height;
}
@Entity
@Data
public class Pig extends Animal {
private int weight;
}
Summarize
This article introduces three commonly used methods of entity inheritance to create tables in Hibernate: single table inheritance strategy, concrete class inheritance strategy, and mapping superclass strategy. By rationally choosing an appropriate inheritance strategy, the inheritance relationship between objects can be flexibly handled and mapped to the database table structure.