Hibernate 一对一主键双向关联
2009-12-04 14:12
281 查看
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。
原文地址:/article/4231426.html
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>
测试很简单就不写了。
本文出自 “熔 岩” 博客,转载请与作者联系!
原文地址:/article/4231426.html
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>
测试很简单就不写了。
本文出自 “熔 岩” 博客,转载请与作者联系!
相关文章推荐
- Hibernate关系映射 --- 一对一实例分析(双向关联,是基于主键的)
- Hibernate 一对一主键双向关联
- hibernate一对一主键关联映射双向
- Hibernate一对一主键双向关联映射(xml配置)
- HIBERNATE一对一双向外键联合主键关联
- hibernate一对一主键双向关联
- Hibernate中的one2one映射实例二(一对一主键关联映射双向)
- 一个Hibernate一对一主键双向关联实例
- hibernate双向一对一主键关联映射XML与注解版
- Hibernate学习笔记_08_一对一单向、双向主键关联
- java鬼混笔记:Hibernate:2、一对一关系之主键单向和双向关联
- hibernate一对一双向主键关联_Annotation
- Hibernate:一对一双向主键关联
- hibernate一对一双向主键关联_XML
- Hibernate之对象关系映射05一对一双向主键关联
- Hibernate关系映射级别注解(一对一双向外键关联、一对一双向外键联合主键)
- Hibernate 第十讲 表关联关系(四)双向一对一主键关联
- Hibernate 一对一主键双向关联
- Hibernate 一对一主键双向关联
- Hibernate一对一主键双向关联