Single Table per Class Hierarchy Strategy: the <sub> element in Hibernate
Joined Sub Strategy: the <joined-sub> element in Hibernate
ejb支持 3种映射关系
1每个类张表 (hibertnate里对应<union->)
2每个类层次张表 (在 hibernate里对应<sub>)
3连接子类(对应join-sub)
目前不支持在接口上进行注解
(1)每个类张表:
在父类-level上设置:@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
例如:
Java代码
A代码:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public A {
private id;
private String aname;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public getId {
id;
}
public void Id( id) {
this.id = id;
}
public String getAname {
aname;
}
public void Aname(String aname) {
this.aname = aname;
}
}
B extends A代码:
@Entity
public B extends A{
private String bname;
public String getBname {
bname;
}
public void Bname(String bname) {
this.bname = bname;
}
}
C extends A代码:
@Entity
public C extends A{
private String cname;
public String getCname {
cname;
}
public void Cname(String cname) {
this.cname = cname;
}
}
最终生成sql语句:
Java代码
create table A (id eger not null auto_increment, aname varchar(255), primary key (id))
create table B (id eger not null, aname varchar(255), bname varchar(255), primary key (id))
create table C (id eger not null, aname varchar(255), cname varchar(255), primary key (id))
B 和 C 都继承了A但是没有关联
(2)每个类层次张表:也就是所有继承类和父类共享张表 通过个辨别符号进行区分
这需要在父类上使用:@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
这样话在表里面会多出个字段:DTYPE(默认 默认值为entry.)
可以通过在父类-level上设置
@DiscriminatorColumn(name="mytype",discriminatorType=DiscriminatorType.STRING)
在的类上使用
例如:
@Entity
@DiscriminatorValue(value="ctype")
插入数据时候将会有下列语句产生:Hibernate: insert o A (aname, cname, mytype) values (?, ?, 'ctype');
(3)每个字类张表:也就是字类关联到父类主键
通过在父类上使用:@Inheritance(strategy=InheritanceType.JOINED)
产生语句:默认id关联
Java代码
create table A (id eger not null auto_increment, aname varchar(255), primary key (id))
create table B (bname varchar(255), id eger not null, primary key (id))
create table C (cname varchar(255), id eger not null, primary key (id))
alter table B add index FK42FCA55807 (id), add constra FK42FCA55807 foreign key (id) references A (id)
alter table C add index FK43FCA55807 (id), add constra FK43FCA55807 foreign key (id) references A (id)
也可以指定关联 例如:在B-level上使用@PrimaryKeyJoinColumn(name="bid")
生成sql语句:
Java代码
create table B (bname varchar(255), bid eger not null, primary key (bid))
alter table B add index FK42FCA6C7E9 (bid), add constra FK42FCA6C7E9 foreign key (bid) references A (id)
但是我们不能关联到A非主键字段例如:
在B上使用
@PrimaryKeyJoinColumn(name="bid",referencedColumnName="aname")则会报错:SecondaryTable JoinColumn cannot reference a non primary key
当然也可以给的类关联设置区别类型例如:@PrimaryKeyJoinColumn(name="bid",columnDefinition="carchar(20)")但是不能设置不能转换类型例如:
@PrimaryKeyJoinColumn(name="bid",columnDefinition="blob")则会建立不了关联
(4)从实体继承 但是父类不持久化:使用@MappedSuper
sql语句:
Java代码
create table B (id eger not null auto_increment, aname varchar(255), bname varchar(255), primary key (id))
create table C (id eger not null auto_increment, aname varchar(255), cname varchar(255), primary key (id))
当然可以使用@AttributeOverride或者@AssociationOverride进行覆盖
最新评论