tatic final long serialVersionUID = -6122347374515830424L;
@Column(name = "hourly_wage")
private Float hourlyWage;
// getter/setter方法
}这会映射成三个具体的表,分别是,Employee对应EMP表,字段包括empId、name;FullTimeEmployee对应FT_EMP表,字段包括empId、salary;PartTimeEmployee对应PT_EMP表,字段包括empId、hourly_wage。其中,表FT_EMP和PT_EMP中的empId和EMP表的empId没有任何关系。子类实体每保存一条数据,EMP表中不会插入记录。
而且主键的生成策略不能使用GenerationType.AUTO或GenerationType.IDENTITY,否则会出现异常:
org.hibernate.MappingException: Cannot use identity column key generation with
mapping for: com.mikan.PartTimeEmployee
因为TABLE_PER_CLASS策略每个表都是单独的,没有并且各表的主键没有任何关系,所以不能使用GenerationType.AUTO或GenerationType.IDENTITY主键生成策略,可以使用GenerationType.TABLE。
具体可参考:http://stackoverflow.com/questions/916169/cannot-use-identity-column-key-generation-with-union-subclass-table-per-clas
如果超类是抽象类,那么不会生成对应的表。如果超类是具体的类,那么会生成对应的表。
以上实例使用JPA的hibernate实现测试通过。