一對一雙向外鍵關聯(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