疯狂

STANDING ON THE SHOULDERS OF GIANTS
posts - 481, comments - 486, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

hibernate annoation (七 继承映射)

Posted on 2009-11-02 14:54 疯狂 阅读(2144) 评论(0)  编辑  收藏 所属分类: hibernate

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代码 复制代码
  1. class A代码:   
  2. @Entity  
  3. @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)   
  4. public class A {   
  5.   
  6.  private int id;   
  7.  private String aname;   
  8.  @Id  
  9.  @GeneratedValue(strategy=GenerationType.IDENTITY)   
  10.  public int getId() {   
  11.   return id;   
  12.  }   
  13.  public void setId(int id) {   
  14.   this.id = id;   
  15.  }   
  16.  public String getAname() {   
  17.   return aname;   
  18.  }   
  19.  public void setAname(String aname) {   
  20.   this.aname = aname;   
  21.  }   
  22.     
  23. }   
  24.   
  25. class B extends A代码:   
  26. @Entity  
  27. public class B extends A{   
  28.   
  29.  private String bname;   
  30.   
  31.  public String getBname() {   
  32.   return bname;   
  33.  }   
  34.   
  35.  public void setBname(String bname) {   
  36.   this.bname = bname;   
  37.  }   
  38.     
  39. }   
  40. class C extends A代码:   
  41. @Entity  
  42. public class C extends A{   
  43.   
  44.  private String cname;   
  45.   
  46.   
  47.  public String getCname() {   
  48.   return cname;   
  49.  }   
  50.   
  51.  public void setCname(String cname) {   
  52.   this.cname = cname;   
  53.  }   
  54.   
  55.     
  56. }  

 

最终生成sql语句:

Java代码 复制代码
  1. create table A (id integer not null auto_increment, aname varchar(255), primary key (id))   
  2. create table B (id integer not null, aname varchar(255), bname varchar(255), primary key (id))   
  3. 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代码 复制代码
  1. create table A (id integer not null auto_increment, aname varchar(255), primary key (id))   
  2. create table B (bname varchar(255), id integer not null, primary key (id))   
  3. create table C (cname varchar(255), id integer not null, primary key (id))   
  4. alter table B add index FK42FCA55807 (id), add constraint FK42FCA55807 foreign key (id) references A (id)   
  5. alter table C add index FK43FCA55807 (id), add constraint FK43FCA55807 foreign key (id) references A (id)  

 
也可以指定关联 例如:在B的class-level上使用@PrimaryKeyJoinColumn(name="bid")
生成sql语句:

Java代码 复制代码
  1. create table B (bname varchar(255), bid integer not null, primary key (bid))   
  2. 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代码 复制代码
  1. create table B (id integer not null auto_increment, aname varchar(255), bname varchar(255), primary key (id))   
  2. create table C (id integer not null auto_increment, aname varchar(255), cname varchar(255), primary key (id))  

 
 当然可以使用@AttributeOverride或者@AssociationOverride进行覆盖


只有注册用户登录后才能发表评论。


网站导航: