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

Hibernate 一对一外键双向关联

2019-11-10 17:57:13
字体:
来源:转载
供稿:网友

Hibernate 一对一外键双向关联

 一对一外键关联是一对多外键关联的特例,只是在多的一方加了个唯一性约束。 一、模型一个人对应一个地址。 /*==============================================================*//* DBMS name:      MySQL 5.0                                    *//* Created on:     2008-12-9 0:12:54                            *//*==============================================================*/drop table if exists address;drop table if exists person;/*==============================================================*//* Table: address                                               *//*==============================================================*/create table address(   id                   bigint not null auto_increment comment 'ID',   detail               varchar(120) not null comment '详细地址',   personid             bigint comment '人的ID',   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_4 foreign key (personid)      references person (id) on delete restrict on 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>    <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="testdb">    <id name="id" type="java.lang.Long">      <columnname="id" />       <generatorclass="identity"/>     </id>    <propertyname="detail"type="java.lang.String">      <columnname="detail"length="120"not-null="true">        <comment>详细地址</comment>      </column>    </property>    <many-to-onename="person"class="entity.Person"      fetch="select"unique="true">      <columnname="personid">        <comment>人的ID</comment>      </column>    </many-to-one>  </class></hibernate-mapping> <?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/testdb     </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>    <mappingresource="entity/Person.hbm.xml"/>     <mappingresource="entity/Address.hbm.xml"/>   </session-factory></hibernate-configuration> 四、测试import org.hibernate.Transaction; import entity.Address; import entity.Person; import utils.HibernateSessionFactory; public class Test {  public staticvoid main(String[] args) {     savePerson();   }   public staticvoid savePerson() {     Person person = new Person("张三");    Address address = new Address("XX街X号");    person.setAddress(address);     address.setPerson(person);     Session session = HibernateSessionFactory.getSession();     Transaction tx = session.beginTransaction();     session.save(person);     tx.commit();   } } 运行日志:Hibernate:            insert            into                 person                 (name)            values                 (?) Hibernate:            insert            into                 testdb.address                 (detail, personid)            values                 (?, ?)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表