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;
/* 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;
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;
private Long id;
private Person person;
private String detail;
四、映射代码
<?
xml
version
="1.0"
encoding
="utf-8"
?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
< hibernate-mapping >
< class name ="entity.Person" table ="person" >
< id name ="id" type ="java.lang.Long" >
< column name ="id" />
< generator class ="identity" />
</ id >
< property name ="name" type ="java.lang.String" >
< column name ="name" length ="24" not-null ="true" >
< comment >姓名 </ comment >
</ column >
</ property >
<!-- cascade="all":在保存person对象的时候,级联保存person对象关联的address对象 -->
< one-to-one name ="address" cascade ="all" />
</ class >
</ hibernate-mapping >
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
< hibernate-mapping >
< class name ="entity.Person" table ="person" >
< id name ="id" type ="java.lang.Long" >
< column name ="id" />
< generator class ="identity" />
</ id >
< property name ="name" type ="java.lang.String" >
< column name ="name" length ="24" not-null ="true" >
< comment >姓名 </ comment >
</ column >
</ property >
<!-- cascade="all":在保存person对象的时候,级联保存person对象关联的address对象 -->
< one-to-one name ="address" cascade ="all" />
</ class >
</ hibernate-mapping >
<?
xml
version
="1.0"
encoding
="utf-8"
?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
< hibernate-mapping >
< class name ="entity.Address" table ="address" catalog ="mydb" >
< id name ="id" type ="java.lang.Long" >
< column name ="id" />
<!-- class="foreign": 一对一主键映射中,使用另外一个相关联的对象的标识符 -->
< generator class ="foreign" >
< param name ="property" >person </ param >
</ generator >
</ id >
< property name ="detail" type ="java.lang.String" >
< column name ="detail" length ="120" not-null ="true" >
< comment >详细地址 </ comment >
</ column >
</ property >
<!-- 表示在address表存在一个外键约束,外键参考相关联的表person -->
< one-to-one name ="person" constrained ="true" />
</ class >
</ hibernate-mapping >
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
< hibernate-mapping >
< class name ="entity.Address" table ="address" catalog ="mydb" >
< id name ="id" type ="java.lang.Long" >
< column name ="id" />
<!-- class="foreign": 一对一主键映射中,使用另外一个相关联的对象的标识符 -->
< generator class ="foreign" >
< param name ="property" >person </ param >
</ generator >
</ id >
< property name ="detail" type ="java.lang.String" >
< column name ="detail" length ="120" not-null ="true" >
< comment >详细地址 </ comment >
</ column >
</ property >
<!-- 表示在address表存在一个外键约束,外键参考相关联的表person -->
< one-to-one name ="person" constrained ="true" />
</ class >
</ hibernate-mapping >
五、Hibernate配置
<?
xml
version
='1.0'
encoding
='UTF-8'
?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" >root </ property >
< property name ="connection.url" >
jdbc:mysql://localhost:3306/mydb
</ property >
< property name ="dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="connection.password" >xiaohui </ property >
< property name ="connection.driver_class" >
com.mysql.jdbc.Driver
</ property >
< property name ="show_sql" >true </ property >
< property name ="format_sql" >true </ property >
< mapping resource ="entity/Person.hbm.xml" />
< mapping resource ="entity/Address.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" >root </ property >
< property name ="connection.url" >
jdbc:mysql://localhost:3306/mydb
</ property >
< property name ="dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="connection.password" >xiaohui </ property >
< property name ="connection.driver_class" >
com.mysql.jdbc.Driver
</ property >
< property name ="show_sql" >true </ property >
< property name ="format_sql" >true </ property >
< mapping resource ="entity/Person.hbm.xml" />
< mapping resource ="entity/Address.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
测试很简单就不写了。
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/39333,如需转载请自行联系原作者