一对一主键映射在一对一映射中还算是最为常用的。
一、模型
一个人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 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
博文链接:https://shaqiang32.iteye.com/blog/201309
NULL 博文链接:https://paladin1988.iteye.com/blog/1639102
Hibernate学习要点:一对一主键关联双向
博文链接:https://llying.iteye.com/blog/220803
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向...├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
NULL 博文链接:https://lijunabc.iteye.com/blog/438426
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
010 一对一 主键关联映射_双向 011 一对一 唯一外键关联映射_单向 012 一对一 唯一外键关联映射_双向 013 session_flush 014 一对多关联映射 单向 015 一对多关联映射 双向 016 多对多关联映射 单向 017 多对多关联...
NULL 博文链接:https://cdxs2.iteye.com/blog/1932867
hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person---->IdCard) 10 hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 11 session ...
Hibernate学习要点:一对一主键关联双向
一对一关联可能是双向的,在双向关联中,有且仅有一端作为主体(owner)端存在:主体端负责维护联接列(即更新),对于不需要维护这种关系的从表则通过mappedNy属性进行声明。mappedBy的值指向主体的关联属性。例子...
Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚
自己记录的学习笔记,内容很详细,有代码,包括:一对多双向关联关系、一对多双向自身关联关系、一对一关联关系(主键关联、外键关联)、一对多与一对一结合、多对多关联关系、Map、Set、List与Bag映射关系、查询...
Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...
7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...
8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一...
一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...