首页 > 学院 > 开发设计 > 正文

Hibernate 一对一主键双向关联

2019-11-10 20:24:33
字体:
来源:转载
供稿:网友
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                                     bigintnot null comment'ID',      detail                             varchar(120)not null comment'详细地址',      PRimary key (id)) type = InnoDB; alter table address comment'地址'; /*==============================================================*//* Table: person                                                                                                *//*==============================================================*/create table person(      id                                     bigintnot null auto_increment comment'ID',      name                                 varchar(24) not null comment '姓名',      primary key (id)) type = InnoDB; alter table person comment'人'; alter table addressadd constraint FK_Reference_2foreign key (id)            references person (id) on delete restricton update restrict; 三、对象模型代码 public class Personimplements java.io.Serializable {   private Long id;   private String name;   private Address address; public class Addressimplements 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">      <columnname="id" />       <generatorclass="identity"/>     </id>    <propertyname="name"type="java.lang.String">      <columnname="name"length="24"not-null="true">        <comment>姓名</comment>      </column>    </property>    <!-- cascade="all":在保存person对象的时候,级联保存person对象关联的address对象    -->    <one-to-onename="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">      <columnname="id" />       <!-- class="foreign": 一对一主键映射中,使用另外一个相关联的对象的标识符-->       <generatorclass="foreign">        <paramname="property">person</param>      </generator>    </id>    <propertyname="detail"type="java.lang.String">      <columnname="detail"length="120"not-null="true">        <comment>详细地址</comment>      </column>    </property>    <!-- 表示在address表存在一个外键约束,外键参考相关联的表person -->    <one-to-onename="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>  <propertyname="connection.username">root</property>  <propertyname="connection.url">    jdbc:mysql://localhost:3306/mydb   </property>  <propertyname="dialect">    org.hibernate.dialect.MySQLDialect   </property>  <propertyname="connection.passWord">xiaohui</property>  <propertyname="connection.driver_class">    com.mysql.jdbc.Driver   </property>  <propertyname="show_sql">true</property>  <propertyname="format_sql">true</property>  <mapping resource="entity/Person.hbm.xml" />   <mapping resource="entity/Address.hbm.xml" /> </session-factory></hibernate-configuration>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表