一,概述
前面几篇博客写了一对多 多对多等常见的映射,今天就来写关于一对一映射吧!现实生活中,人与身份信息就是一对一映射.
二,JavaBean设计
1)User.java
package com.bighuan.c_one2one;
public class User {
private int userId;
private String userName;
private IdCard idCard;
public User() {
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public IdCard getIdCard() {
return idCard;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", idCard=" + idCard + "]";
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
2)IdCard.java
package com.bighuan.c_one2one;
public class IdCard {
private String cardNum;// 主键
private String place;
private User user;
public IdCard() {
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "IdCard [cardNum=" + cardNum + ", place=" + place + ", user="
+ user + "]";
}
}
三,基于外键的映射
1)User.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="com.bighuan.c_one2one">
<!-- 对象与表对应 -->
<class name="User" table="t_user">
<id name="userId">
<generator class="native" />
</id>
<property name="userName" length="20"></property>
<!-- 一对一映射:没有外键方 -->
<one-to-one name="idCard" class="IdCard"></one-to-one>
</class>
</hibernate-mapping>
2)IdCard.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="com.bighuan.c_one2one">
<!-- 对象与表对应 -->
<class name="IdCard" table="t_IdCard">
<id name="cardNum">
<!-- 手动指定主键 -->
<generator class="assigned" />
</id>
<property name="place" length="20"></property>
<!-- 一对一映射:有外键方
unique="true":给外键添加唯一约束
-->
<many-to-one name="user" unique="true" class="User" column="user_id" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
四,测试一
1)代码
package com.bighuan.c_one2one;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App {
private static SessionFactory sf;
static {
sf = new Configuration().configure().addClass(IdCard.class)
.addClass(User.class)// 测试时用
.buildSessionFactory();
}
@Test
public void testSave() throws Exception {
// 获得session
org.hibernate.Session session = sf.openSession();
session.beginTransaction();
//用户
User user=new User();
user.setUserName("Leslie");
//身份证
IdCard idCard=new IdCard();
idCard.setCardNum("360732XXX");
idCard.setPlace("江西南昌");
//关系
idCard.setUser(user);
//保存
session.save(idCard);
session.getTransaction().commit();
session.close();
}
}
2)结果
用户表有两个字段:userId userName,身份证表有三个字段:cardNum place user_id,第一个字段为主键.
五,基于主键的映射
1)User.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="com.bighuan.c_one2one2">
<!-- 对象与表对应 -->
<class name="User" table="t_user">
<id name="userId">
<generator class="native" />
</id>
<property name="userName" length="20"></property>
<!-- 一对一映射:没有外键方 -->
<one-to-one name="idCard" class="IdCard"></one-to-one>
</class>
</hibernate-mapping>
2)IdCard.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="com.bighuan.c_one2one2">
<!-- 对象与表对应 -->
<class name="IdCard" table="t_IdCard">
<id name="user_id">
<!--
id 节点指定的是主键映射, 即user_id是主键
主键生成方式: foreign 即把别的表的主键作为当前表的主键;
property (关键字不能修改)指定引用的对象 对象的全名 cn..User、
对象映射 cn.User.hbm.xml、 table(id)
-->
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="place" length="20"></property>
<property name="cardNum" length="20"></property>
<!--
一对一映射,有外键方
(基于主键的映射)
constrained="true" 指定在主键上添加外键约束
-->
<one-to-one name="user" class="User" constrained="true" cascade="save-update"></one-to-one>
</class>
</hibernate-mapping>
六,测试二
1)代码
package com.bighuan.c_one2one2;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App {
private static SessionFactory sf;
static {
sf = new Configuration().configure().addClass(IdCard.class)
.addClass(User.class)// 测试时用
.buildSessionFactory();
}
@Test
public void testSave() throws Exception {
// 获得session
org.hibernate.Session session = sf.openSession();
session.beginTransaction();
//用户
User user=new User();
user.setUserName("Leslie111");
//身份证
IdCard idCard=new IdCard();
idCard.setCardNum("360732XXXYYYY");
idCard.setPlace("江西南昌");
//关系
idCard.setUser(user);
//保存
session.save(idCard);
session.getTransaction().commit();
session.close();
}
}
2)结果
用户表有两个字段:userId userName,身份证表有三个字段:user_id cardNum place ,第一个字段为主键.
七,总结
两种映射方式生成的表有一点区别,主要是身份证表的主键不一样.