hibernate(六)一对一映射
关系映射是指对象之间的关系,并不是指数据库的关系,关系映射是解决当对象处于以下关系之一时,数据库表该如何映射的问题
(一)一对一单向外键关联
1、注解方式配置
创建一个Husband类和Wife类
Husband类:(getWife方法上加注解OneToOne)
package cn.orlion.hibernate.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne; @Entity
public class Husband { private int id; private String name; private Wife wife; @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
@OneToOne
@JoinColumn(name="wifeId")//默认生成的外键id是wife_id在这里使用JoinColumn注解name属性指定为wifeId
public Wife getWife() {
return wife;
} public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife类
package cn.orlion.hibernate.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class Wife { private int id; private String name; @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
然后创建一个测试类HibernateMappingTest
package cn.orlion.hibernate.model; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test; public class HibernateORMappingTest { @Test
public void testSchemaExport(){ new SchemaExport(new AnnotationConfiguration().configure()).create(true , true);
} }
运行testSchemaExport可以在控制台看到建表语句:
2、xml方式配置
首先创建一个Student类和StuIdCard(学生证)
Student类:
package cn.orlion.hibernate.model; public class Student { private int id; private String name; private String sex; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}
StuIdCard:
package cn.orlion.hibernate.model; public class StuIdCard { private int id; private int num; private Student student; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} public Student getStudent() {
return student;
} public void setStudent(Student student) {
this.student = student;
}
}
然后创建两个配置文件Student.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.orlion.hibernate.model">
<class name="cn.orlion.hibernate.model.Student">
<id name="id">
<generator class="native"></generator>
</id> <property name="name"></property> <property name="sex"></property>
</class>
</hibernate-mapping>
StuIdCard.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.orlion.hibernate.model">
<class name="cn.orlion.hibernate.model.StuIdCard">
<id name="id">
<generator class="native"></generator>
</id> <property name="num" /> <many-to-one name="student" column="studentId" unique="true"></many-to-one>
<!-- many-to-one是当前类即StuIdCard类相对于Student类是多对一关系 (多个学生证对应一个学生) ,unique=true限定为了一对一 -->
</class>
</hibernate-mapping>
然后运行测试类HibernateORMappingTest类的testSchemaExport方法可以在控制台看到建表语句:
(二)一对一双向外键关联
1、注解方式设置
双向外键关联注解方式设置是在wife类中添加husband属性,在getHusband()方法上添加注解@OneToOne(mappedBy="wife")//mappedBy="wife"表示Husband中wife属性已经做映射了,不用管当前类的设置(不再从wife表中生成指向husband的外键)注:如果不写mappedBy="wife"会在wife表中生成一个husband_id外键,这是多余的。
Wife类:
package cn.orlion.hibernate.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne; @Entity
public class Wife { private int id; private String name; private Husband husband; @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
@OneToOne(mappedBy="wife")//mappedBy="wife"表示Husband中wife属性已经做映射了,不用管当前类的设置(不再从wife表中生成指向husband的外键)
public Husband getHusband() {
return husband;
} public void setHusband(Husband husband) {
this.husband = husband;
} }
2、xml方式配置
在Student.hbm.xml配置文件中添加<one-to-one name="stuIdCard" property-ref="student"></one-to-one>,在Student类中添加stuIdCard属性:
Student类:
package cn.orlion.hibernate.model; public class Student { private int id; private String name; private String sex; private StuIdCard stuIdCard; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public StuIdCard getStuIdCard() {
return stuIdCard;
} public void setStuIdCard(StuIdCard stuIdCard) {
this.stuIdCard = stuIdCard;
} }
Student.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.orlion.hibernate.model">
<class name="cn.orlion.hibernate.model.Student">
<id name="id">
<generator class="native"></generator>
</id> <property name="name"></property> <property name="sex"></property> <one-to-one name="stuIdCard" property-ref="student"></one-to-one>
<!-- one-to-one只会在一边生成主键,如果这里设置为many-to-one会在student表中生成多余的stuidcard_id外键 -->
<!-- property-ref="student" 类似于mappedby="student"是指在StuIdCard中student属性已经设置好了关联关系 -->
</class>
</hibernate-mapping>
注:一对一单向和双向在数据库中表现没有区别
主键关联是指的一个表的主键和另外一个表的主键关联
外键关联是指的一个表的主键和另外一个表的非主键关联
(三)一对一单向主键关联
xml方式配置:
创建Student类
package cn.orlion.hibernate.model; public class Student { private int id; private String name; private String sex; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}
StuIdCard类:
package cn.orlion.hibernate.model; public class StuIdCard { private int id; private int num; private Student student; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} public Student getStudent() {
return student;
} public void setStudent(Student student) {
this.student = student;
}
}
Student.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.orlion.hibernate.model">
<class name="cn.orlion.hibernate.model.Student">
<id name="id">
<generator class="native"></generator>
</id> <property name="name"></property> <property name="sex"></property>
</class>
</hibernate-mapping>
StuIdCard.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.orlion.hibernate.model">
<class name="cn.orlion.hibernate.model.StuIdCard">
<id name="id">
<generator class="foreign"><!-- StuIdCard的主键应该靠外键来生成,StuIdCard -->
<param name="property">student</param><!-- 依靠student属性的关联关系 -->
</generator>
</id> <property name="num" /> <one-to-one name="student" constrained="true"></one-to-one>
<!-- constrained="true"用来生成外键约束,不加这个属性就不会加外键 -->
</class>
</hibernate-mapping>
运行:
(四)一对一双向主键关联
注解方式配置:
Husband类:
package cn.orlion.hibernate.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn; @Entity
public class Husband { private int id; private String name; private Wife wife; @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Wife getWife() {
return wife;
} public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife类:
package cn.orlion.hibernate.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne; @Entity
public class Wife { private int id; private String name; private Husband husband; @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Husband getHusband() {
return husband;
} public void setHusband(Husband husband) {
this.husband = husband;
} }
(五)联合主键映射
Wife类:
package cn.orlion.hibernate.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass; @Entity
@IdClass(WifePK.class)
public class Wife { private int id; private String name; private String age; @Id
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
}
@Id
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} }
WifePK:
package cn.orlion.hibernate.model; public class WifePK implements java.io.Serializable{ private int id; private String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
Husband类:
package cn.orlion.hibernate.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne; @Entity
public class Husband { private int id; private String name; private Wife wife; @Id
@GeneratedValue
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
@OneToOne
@JoinColumns(
{
@JoinColumn(name="wifeId" , referencedColumnName="id"),
@JoinColumn(name="wifeName" , referencedColumnName="name")
}
)
public Wife getWife() {
return wife;
} public void setWife(Wife wife) {
this.wife = wife;
}
}
运行结果:
hibernate(六)一对一映射的更多相关文章
- hibernate中一对一映射
一.hibernate中一对一映射有两种 1 主键方式,一张表的主键是通过另一张表的主键生成的 2 外键方式,一张表添加外键引用另一张表的主键,并添加唯一unique约束 二.下面进行简单例子,用户和 ...
- 9.hibernate的一对一映射
一.hibernate的一对一按照外键映射 1.创建如下sql脚本: --员工账号表 create table USERS1 ( USERID ) not null, USERNAME ), USER ...
- Hibernate的一对一映射
一.创建Java工程,新建Lib文件夹,加入Hibernate和数据库(如MySql.Oracle.SqlServer等)的Jar包,创建 hibernate.cfg.xml 文件,并配置,配置项如下 ...
- Hibernate(七)一对一映射
一.创建数据库表 --班级表 create table grade ( gid number primary key, --班级ID gname ), --班级名称 gdesc ) --班级介绍 ); ...
- Hibernate表关系映射之一对一映射
一.数据表的映射关系 在数据库领域中,数据表和数据表之间关系一般可以分为如下几种: 一对一:比如公民和身份证的关系,一个人只有一张身份证,同时每张身份证也仅仅对应一个人! 一对多:比如客户和订单之间的 ...
- hibernate笔记--基于主键的单(双)向的一对一映射关系
上一节介绍的基于外键的一对一映射关系中,在Person表中有一个外键列idCard_id,对应的idCard表的主键id,至于基于主键的一对一映射关系,就是指Person表中抛弃了idcard_id这 ...
- hibernate笔记--基于外键的单(双)向的一对一映射关系
假设我们有两张表,人员信息表Person,和身份信息表IdCard,我们知道每个人只有一个身份证号,所以这里的Person和IdCard表是一一对应的,也就是一对一的映射关系,基于外键的单向一对一映射 ...
- Hibernate一对一映射关联
Hibernate提供了两种一对一映射关联关系的方式: 1)按照外键映射 2)按照主键映射 下面以员工账号表和员工档案表(员工账号和档案表之间是一对一的关系)为例,介绍这两种映射关系,并使用这两种 映 ...
- Hibernate中的一对一映射
1.需求 用户和身份证是一一对应的关系. 有两种对应方式: 用户id作为身份证表的外键,身份证号作为主键: 用户id作为身份证表的主键: 2.实体Bean设计 User: public class U ...
- 【Hibernate 3】一对一映射配置
一.一对一映射简介 一对一关联映射有两种实现策略: 第一种:主键关联,即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库表不会有额外的字段来维护它们之间的关系,仅通过表的主键来关联 ...
随机推荐
- bash和Bourne_shell的区别
Linux 中的 shell 有很多类型,其中最常用的几种是: Bourne shell (sh).C shell (csh) 和 Korn shell (ksh), 各有优缺点.Bourne she ...
- dataTables-使用详细说明整理
本文共四部分:官网 | 基本使用|遇到的问题|属性表 一:官方网站:[http://www.datatables.net/] 二:基本使用:[http://www.guoxk.com/node/jqu ...
- RHEL6.5 删除桌面启动器(计算机/Home/回收站)
首先,安装gconf-editor以获得gconftool-2命令 终端命令: gconftool-2 --set /apps/nautilus/desktop/computer_icon_visib ...
- Alamofire源码学习
Core文件夹: Alamofire.swift - - - 该文件中主要是给用户提供一些便利的调用方法,用户可以直接调用该文件中的便利方法来使用Alamofire相关功能. ...
- Windows下Oracle安装图解----oracle-win-64-11g 详细安装步骤
一. Oracle 下载 官方下地址 http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.htm ...
- 编译PHP5缺t1lib包安装
t1lib_doc.dvi Error解决: 1. 下载t1lib-5.1.2.tar.gz2. 解决依赖关系:sudo apt-get build-dep t1lib 3. 安装:./configu ...
- 常用正则表达式-copy
匹配中文:[\u4e00-\u9fa5] 英文字母:[a-zA-Z] 数字:[0-9] 匹配中文,英文字母和数字及_: ^[\u4e00-\u9fa5_a-zA-Z0-9]+$ 同时判断输入长度:[\ ...
- Vmware配置
1.网络 四种连接模式的区别:http://www.cnblogs.com/awpatp/archive/2009/07/24/1530471.html 如果网卡使用Nat模式,则需要启动服务: VM ...
- 【腾讯Bugly干货分享】移动互联网测试到质量的转变
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ee0934b690d84c3188d7c7 Dev Club 是一个交流移动 ...
- HTML目录生成工具
目录 内容简介 工具代码 使用方法 工具改进 内容简介 园子里面很多博主都会为自己的博文创建目录,方便大家浏览.我很好奇大家是怎么做的,是不是有自动生成目录的工具可以推荐一下(我知道word可以,但是 ...