JPA中的实体类可以使用哪些注解来定义继承映射?

JPA中的实体类可以使用@Inheritance注解来定义继承映射。该注解有三种继承策略:

  1. SINGLE_TABLE:单表继承策略。父类和子类实体使用同一表,通过类型字段(@DiscriminatorColumn)区分不同的实体。
  2. TABLE_PER_CLASS:表-每-类继承策略。每一个实体类对应一张表,父类实体属性映射到各个子类表中。
  3. JOINED:联表继承策略。父类实体和子类实体各自对应一张表,并通过中间表关联维护继承关系。

代码示例:
SINGLE_TABLE:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public class Person {
    ...
}

@Entity 
public class Employee extends Person {
   ...
}

@Entity
public class Customer extends Person {
   ...  
} 

TABLE_PER_CLASS:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Person {
   ...
}

@Entity
public class Employee extends Person {
   ...  
}

@Entity 
public class Customer extends Person{
   ...
}

JOINED:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
   ...
}

@Entity
public class Employee extends Person {
   ...  
}

@Entity
public class Customer extends Person{
   ... 
}