读书人

Hibernate 一对一主键双向联系关系

发布时间: 2012-08-08 14:32:45 作者: rapoo

Hibernate 一对一主键双向关联
一对一主键映射在一对一映射中还算是最为常用的。?一、模型?一个人Person 对应一个地址Address。?二、数据模型和对象模型图?Hibernate 一对一主键双向联系关系?导出建表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;?四、映射代码<?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> ?<?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" />
??????<!--
????????????????????"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>

读书人网 >软件架构设计

热点推荐