hibernate annoation (七 担任映射)
副标题#e#
Table per Class Strategy: the <union-class> element in Hibernate
Single Table per Class Hierarchy Strategy: the <subclass> element in Hibernate
Joined Subclass Strategy: the <joined-subclass> element in Hibernate
ejb支持三种映射干系
1,每个类一张表 (hibertnate里对应<union-class>)
2,每个类条理一张表 (在 hibernate里对应<subclass>)
3,毗连的子类(对应join-subclass)
今朝不支持在接口长举办注解
(1)每个类一张表:
在父类class-level上配置:@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
譬喻:
Java代码
class A代码:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class A {
private int id;
private String aname;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
}
class B extends A代码:
@Entity
public class B extends A{
private String bname;
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
}
class C extends A代码:
@Entity
public class C extends A{
private String cname;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
#p#副标题#e#
最终生成sql语句:
Java代码
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))
B 和 C 都担任了A可是没有关联
(2)每个类条理一张表:也就是所有担任的类和父类共享一张表 通过一个分辨标记举办区分
这需要在父类上利用:@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
这样的话在表内里会多出一个字段:DTYPE(默认 默认值为entry.class)
可以通过在父类class-level上配置
@DiscriminatorColumn(name="mytype",discriminatorType=DiscriminatorType.STRING)
在之类上利用
譬喻:
@Entity
@DiscriminatorValue(value="ctype")
插入数据时候将会有下列语句发生:Hibernate: insert into A (aname, cname, mytype) values (?, ?, ‘ctype’);
(3)每个字类一张表:也就是字类的关联到父类的主键
通过在父类上利用:@Inheritance(strategy=InheritanceType.JOINED)
发生语句:默认id关联
Java代码
create table A (id integer not null auto_increment, aname varchar(255), primary key (id))
create table B (bname varchar(255), id integer not null, primary key (id))
create table C (cname varchar(255), id integer not null, primary key (id))
alter table B add index FK42FCA55807 (id), add constraint FK42FCA55807 foreign key (id) references A (id)
alter table C add index FK43FCA55807 (id), add constraint FK43FCA55807 foreign key (id) references A (id)
也可以指定关联 譬喻:在B的class-level上利用@PrimaryKeyJoinColumn(name="bid")
生成sql语句:
Java代码
create table B (bname varchar(255), bid integer not null, primary key (bid))
alter table B add index FK42FCA6C7E9 (bid), add constraint 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)从实体担任 可是父类不耐久化:利用@MappedSuperclass
sql语句:
Java代码
#p#分页标题#e#
create table B (id integer not null auto_increment, aname varchar(255), bname varchar(255), primary key (id))
create table C (id integer not null auto_increment, aname varchar(255), cname varchar(255), primary key (id))
虽然可以利用@AttributeOverride可能@AssociationOverride举办包围