Hibernate 一对一主键双向关联
Hibernate 一对一主键双向关联
一对一主键映射在一对一映射中还算是最为常用的。
一、模型
一个人Person 对应一个地址Address。
二、数据模型和对象模型图
导出建表SQL如下:
/==============================================================/ /* DBMS name: MySQL 5.0 / / Created on: 2008-12-8 23:05:32 / /==============================================================*/
drop table if exists address;
drop table if exists person;
/==============================================================/ /* Table: address / /==============================================================*/ create table address ( id bigint not null comment 'ID', detail varchar(120) not null comment '详细地址', primary key (id) ) type = InnoDB;
alter table address comment '地址';
/==============================================================/ /* Table: person / /==============================================================*/ create table person ( id bigint not null auto_increment comment 'ID', name varchar(24) not null comment '姓名', primary key (id) ) type = InnoDB;
alter table person comment '人';
alter table address add constraint FK_Reference_2 foreign key (id) references person (id) on delete restrict on update restrict;
三、对象模型代码
public class Person implements java.io.Serializable {
private Long id; private String name; private Address address;
public class Address implements java.io.Serializable { private Long id; private Person person; private String detail;
四、映射代码
五、Hibernate配置
测试很简单就不写了。
本文出自 “熔 岩” 博客,转载请与作者联系!