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)
例如:
- 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;
- }
- }
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; } }
最终生成sql语句:
- 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))
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关联
- 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)
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语句:
- 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)
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语句:
- 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))
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进行覆盖