用一个简单的子类继承它:- package persistence2;
- import javax.persistence.*;
- @Entity
- public class MySimpleDerived extends MySimpleEntity {
- private Long derivedId;
- private String field03;
- /*@Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- public Long getDerivedId() {
- return derivedId;
- }
- */
- public void setDerivedId(Long i){
- this.derivedId = i;
- }
- public String getField03(){
- return field03;
- }
- public void setField03(String f){
- this.field03 = f;
- }
- }
复制代码 再看分别存贮MySimpleEntity和MySimpleDerived的对象的结果是:- create table MySimpleEntity (DTYPE varchar(31) not null, id bigint generated by default as identity (start with 1), field01 varchar(255), field02 double, field03 varchar(255), primary key (id))
- INSERT INTO MYSIMPLEENTITY VALUES('MySimpleEntity',1,'Hello world',2.13E0,NULL)
- INSERT INTO MYSIMPLEENTITY VALUES('MySimpleDerived',2,'Hello derived',3.42E0,'I''m derived')
复制代码 总结:
1. 父子两个类采用同一张表
2. 表中第一项为DTYPE,即Data Type,对应存放类名
这样就容易理解为什么在子类中不能再声明@Id子段:因为是同一张表,不能有两个@Id |