WEB开发网
开发学院软件开发Java hibernate annoation (八 关联映射) 阅读

hibernate annoation (八 关联映射)

 2009-09-18 00:00:00 来源:WEB开发网   
核心提示: 如果没有写@JoinColumn(name="b")则默认是关联属性名+下划线+id最终sql:Java代码 createtableA(idintegernotnullauto_increment,anamevarchar(255),b_idinteger,primaryke

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

最终sql:

Java代码

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

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

最终sql

Java代码  

create table A (id integer not null auto_increment, aname varchar(255), b_bname varchar(255), primary key (id)) 
create table B (id integer not null auto_increment, bname varchar(255), primary key (id), unique (bname)) 
alter table A add index FK41E47CD6BD (b_bname), add constraint 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 integer not null auto_increment, aname varchar(255), primary key (id)) 
create table B (id integer not null auto_increment, bname varchar(255), primary key (id)) 
create table centert (bid integer, aid integer not null, primary key (aid)) 
alter table centert add index FK27A6BEBFFCA6C7EA (bid), add constraint FK27A6BEBFFCA6C7EA foreign key (bid) references B (id) 
alter table centert add index FK27A6BEBFFCA6C428 (aid), add constraint FK27A6BEBFFCA6C428 foreign key (aid) references A (id) 

Tags:hibernate annoation 关联

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接