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;
 
四、映射代码
<? 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" />
      <!-- 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 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值