专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » hibernate映射:hibernate annoation ( 8 关联映射) »正文

hibernate映射:hibernate annoation ( 8 关联映射)

来源: 发布时间:星期四, 2009年9月24日 浏览:48次 评论:0
  onetoone:单向

  1,主键关联:

  在关联放使用@OneToOne

  sql语句:(类代码见同前面代码)

  Java代码

create table A (id eger not null auto_increment, aname varchar(255), b_id eger, primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), primary key (id)) 
alter table A add index FK41FCD34905 (b_id), add constra FK41FCD34905 foreign key (b_id) references B (id) 


  可以使用@PrimaryKeyJoinColumn进行关联

  2 双向:

  在关联方使用 

  @OneToOne(cascade=CascadeType.ALL)

  @JoinColumn(name="b")

  被关联方使用

  @OneToOne(mappedBy="b")

  最终sql:

  Java代码 create table A (id eger not null auto_increment, aname varchar(255), b eger, primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), primary key (id)) 
alter table A add index FK41FCA54B4F (b), add constra FK41FCA54B4F foreign key (b) references B (id)


  如果不写

  @OneToOne(mappedBy="b")则会在被关联放也生成个字段

  最终代码:

  Java代码  

create table A (id eger not null auto_increment, aname varchar(255), i_id eger, primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), a_id eger, primary key (id)) 
alter table A add index FK41FCD6779E (i_id), add constra FK41FCD6779E foreign key (i_id) references B (id) 
alter table B add index FK42FCD2D4A5 (a_id), add constra FK42FCD2D4A5 foreign key (a_id) references A (id)


  如果没有写@JoinColumn(name="b")则默认是关联属性名+下划线+id

  最终sql:

  Java代码

create table A (id eger not null auto_increment, aname varchar(255), b_id eger, primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), primary key (id)) 
alter table A add index FK41FCD34905 (b_id), add constra FK41FCD34905 foreign key (b_id) references B (id)


  可以使用@JoinColumn(referencedColumnName="bname")让主关联方不关联被关联放主键

  最终sql

  Java代码  

create table A (id eger not null auto_increment, aname varchar(255), b_bname varchar(255), primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), primary key (id), unique (bname)) 
alter table A add index FK41E47CD6BD (b_bname), add constra FK41E47CD6BD foreign key (b_bname) references B (bname)


  3 关联表

  使用

@OneToOne(cascade=CascadeType.ALL) 
 @JoinTable(name="centert",joinColumns=@JoinColumn(name="aid"),inverseJoinColumns=@JoinColumn(name="bid"))


  最终sql:

  写道

create table A (id eger not null auto_increment, aname varchar(255), primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), primary key (id)) 
create table centert (bid eger, aid eger not null, primary key (aid)) 
alter table centert add index FK27A6BEBFFCA6C7EA (bid), add constra FK27A6BEBFFCA6C7EA foreign key (bid) references B (id) 
alter table centert add index FK27A6BEBFFCA6C428 (aid), add constra FK27A6BEBFFCA6C428 foreign key (aid) references A (id) 


  manytoone

  和onetoone很相似

  特殊情况:果不写:mappedBy这会产生中间表:

  Java代码  

create table A (id eger not null auto_increment, aname varchar(255), i_id eger, primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), primary key (id)) 
create table B_A (B_id eger not null, a_id eger not null, unique (a_id)) 
alter table A add index FK41FCD6779E (i_id), add constra FK41FCD6779E foreign key (i_id) references B (id) 
alter table B_A add index FK10384FCD34905 (B_id), add constra FK10384FCD34905 foreign key (B_id) references B (id) 
alter table B_A add index FK10384FCD2D4A5 (a_id), add constra FK10384FCD2D4A5 foreign key (a_id) references A (id)


  如

  targetEntity属性可以关联接口

  例如接口代码

  Java代码   

public erface I { 
 
}


   B implments I

  关联方:

  Java代码   

private I i; 
@ManyToOne(targetEntity=B.) 
 public I getI { 
  i; 
 }


  最终sql:

  Java代码

create table A (id eger not null auto_increment, aname varchar(255), i_id eger, primary key (id)) 
create table B (id eger not null auto_increment, bname varchar(255), primary key (id)) 
alter table A add index FK41FCD6779E (i_id), add constra FK41FCD6779E foreign key (i_id) references B (id)


0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: