我是靠谱客的博主 跳跃中心,最近开发中收集的这篇文章主要介绍一对一映射,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、一对一映射

1.需求

用户与身份证信息,一条用户记录对应一条身份证信息!一对一关系!

2.基于外键的映射实现

1.身份证IdCard信息

/**
* 身份证
* Created by qianxingzhe on 16/5/28.
*/
public class IdCard {
/**
* 身份证号(主键),对象唯一标识
*/
private String cardNum;
/**
* 身份证地址
*/
private String place;
/**
* 身份证与用户,一对一的关系
*/
private User user;
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 +
'}';
}
}

2.用户信息

/**
* 用户
* Created by qianxingzhe on 16/5/28.
*/
public class User {
private int userId;
private String userName;
/**
* 用户与身份证信息,一对一关系
*/
private IdCard idCard;
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;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + ''' +
", idCard=" + idCard +
'}';
}
}

3.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.yly.one2one" auto-import="true">
<class name="IdCard" table="t_IdCard">
<id name="cardNum">
<generator class="assigned"/>
</id>
<property name="place" length="20"/>
<!--
一对一映射,有外键方
unique="true" 给外键字段添加唯一约束
-->
<many-to-one name="user" class="User" column="user_id" cascade="save-update" unique="true"></many-to-one>
</class>
</hibernate-mapping>

4.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.yly.one2one">
<class name="User" table="t_user_one2one">
<id name="userId">
<generator class="native"/>
</id>
<property name="userName" length="20"/>
<!--
一对一映射,没有外键方
-->
<one-to-one name="idCard" class="IdCard"></one-to-one>
</class>
</hibernate-mapping>
3.基于主键的映射

1.身份证信息


/**
* 身份证
* Created by qianxingzhe on 16/5/28.
*/
public class IdCard {
private int user_id;
/**
* 身份证号
*/
private String cardNum;
/**
* 身份证地址
*/
private String place;
/**
* 身份证与用户,一对一的关系
*/
private User user;
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;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
@Override
public String toString() {
return "IdCard{" +
"user_id=" + user_id +
", cardNum='" + cardNum + ''' +
", place='" + place + ''' +
", user=" + user +
'}';
}
}

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.yly.one2one" auto-import="true">
<class name="IdCard" table="t_IdCard">
<id name="user_id">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="cardNum" length="20"/>
<property name="place" length="20"/>
<!--
一对一映射,有外键方
(基于主键的映射)
constrained="true" 指定在主键上添加外键约束
-->
<one-to-one name="user" class="User" constrained="true" cascade="save-update"></one-to-one>
</class>
</hibernate-mapping>

转载于:https://www.cnblogs.com/FlySheep/p/5536936.html

最后

以上就是跳跃中心为你收集整理的一对一映射的全部内容,希望文章能够帮你解决一对一映射所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部