一对一双向外键关联(annotation方法):
一夫(husband)一妻(wife)的一对一双向外键关联
husband和wife实体类:
生成的建表语句:
alter table husband
drop
foreign key fkaeea401bc6294ced
alter table wife
foreign key fk29233185d75bea
drop table if exists husband
drop table if exists wife
create table husband (
id integer not null auto_increment,
name varchar(255),
wifeid integer,
primary key (id)
)
create table wife (
husband_id integer,
alter table husband
add index fkaeea401bc6294ced (wifeid),
add constraint fkaeea401bc6294ced
foreign key (wifeid)
references wife (id)
add index fk29233185d75bea (husband_id),
add constraint fk29233185d75bea
foreign key (husband_id)
references husband (id)
双向关联在数据库中只需一边设置外键就行了,所以wife的husband_id就可以不要
mappedby="wife"告诉hibernate,wife和husband之间是一个1对1的关联,
对方husband类的wife属性已经做映射了,你就不用管我生成的外键的关联了
对方那边是主导,你不用管我这边的设置
这个时候wife建表语句中就没有husband_id属性了
凡是双向关联必设mappedby
所以wife实体类注解写成:
@onetoone(mappedby="wife")
public husband gethusband() {
return husband;
}
生成的表中wife就没有husband_id字段了
转载请注明出处:http://blog.csdn.net/acmman