在基类使用TABLE_PER_CLASS映射时,只需要指定所映射的表和映射策略,而不需要设置@DiscriminatorColumn 和@DiscriminatorValue,以下为EmployeeEO的代码:
@Entity
@Table(name = "tb_employee")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@TableGenerator(
name = "employee_gen",
table = "tb_generator",
pkColumnName = "gen_name",
valueColumnName = "gen_value",
pkColumnValue = "employee_id",
allocationSize = 1
)
public class EmployeeEO implements Serializable {
private Integer id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "employee_gen")
public Integer getId() {
return id;
}
//getter和setter方法省略
}
注意,这里除了改变了继承策略外,也更改了主键的生成策略,使用表生成主键。如果使用自动生成主键,子表的主键值可能会与父表的主键值冲突,导致不能正确的创建实体。