註解:父類 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
特點:每個實體類對應一張表,子類表包含父類所有字段,無關聯關係。
create table A (id integer not null auto_increment, aname varchar(255), primary key (id))
create table B (id integer not null, aname varchar(255), bname varchar(255), primary key (id))
create table C (id integer not null, aname varchar(255), cname varchar(255), primary key (id))
註解:父類 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
特點:所有類共享一張表,通過鑑別器列區分。默認鑑別器列為 DTYPE,可自定義:
@DiscriminatorColumn(name="mytype", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="ctype")
插入時生成:insert into A (aname, cname, mytype) values (?, ?, 'ctype')
註解:父類 @Inheritance(strategy=InheritanceType.JOINED)
特點:子類表僅含自身字段,通過主鍵關聯父表。默認主鍵關聯,可自定義:
@PrimaryKeyJoinColumn(name="bid")
-- 生成: create table B (bname varchar(255), bid integer not null, primary key (bid))
限制:不能關聯父類非主鍵字段,列類型必須兼容。
註解:父類 @MappedSuperclass
特點:父類不生成表,字段合併到子類表中。支持 @AttributeOverride 覆蓋映射。