一、需求: 用户与身份证信息
一条用户记录对应一条身份证信息! 一对一的关系!
设计数据库:
JavaBean:
映射:
二、基于外键的映射
- <span style="font-family:KaiTi_GB2312;">// 身份证
- public class IdCard {
- // 身份证号(主键)
- private String cardNum;// 对象唯一表示(Object Identified, OID)
- private String place; // 身份证地址
- // 身份证与用户,一对一的关系
- private User user;</span>
- <span style="font-family:KaiTi_GB2312;">// 用户
- public class User {
- private int userId;
- private String userName;
- // 用户与身份证信息, 一对一关系
- private IdCard idCard;</span>
IdCard.hbm.xml有外键方:
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.c_one2one">
- <class name="IdCard" table="t_IdCard">
- <id name="cardNum">
- <generator class="assigned"></generator>
- </id>
- <property name="place" length="20"></property>
- <!--
- 一对一映射,有外键方
- unique="true" 给外键字段添加唯一约束
- -->
- <many-to-one name="user" unique="true" column="user_id" class="User" cascade="save-update"></many-to-one>
- </class>
- </hibernate-mapping></span>
User.hbm.xml无外键方
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.c_one2one">
- <class name="User" table="t_user">
- <id name="userId">
- <generator class="native"></generator>
- </id>
- <property name="userName" length="20"></property>
- <!--
- 一对一映射: 没有外键方
- -->
- <one-to-one name="idCard" class="IdCard"></one-to-one>
- </class>
- </hibernate-mapping>
- </span>
三、基于主键的映射
- <span style="font-family:KaiTi_GB2312;">// 身份证
- public class IdCard {
- private int user_id;
- // 身份证号
- private String cardNum;
- private String place; // 身份证地址
- // 身份证与用户,一对一的关系
- private User user;</span>
IdCard.hbm.xml
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.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="cardNum" length="20"></property>
- <property name="place" length="20"></property>
- <!--
- 一对一映射,有外键方
- (基于主键的映射)
- constrained="true" 指定在主键上添加外键约束
- -->
- <one-to-one name="user" class="User" constrained="true" cascade="save-update"></one-to-one>
- </class>
- </hibernate-mapping>
- </span>
User.hbm.xml
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.c_one2one2">
- <class name="User" table="t_user">
- <id name="userId">
- <generator class="native"></generator>
- </id>
- <property name="userName" length="20"></property>
- <!--
- 一对一映射: 没有外键方
- -->
- <one-to-one name="idCard" class="IdCard"></one-to-one>
- </class>
- </hibernate-mapping>
- </span>
一、需求: 用户与身份证信息
一条用户记录对应一条身份证信息! 一对一的关系!
设计数据库:
JavaBean:
映射:
二、基于外键的映射
- <span style="font-family:KaiTi_GB2312;">// 身份证
- public class IdCard {
- // 身份证号(主键)
- private String cardNum;// 对象唯一表示(Object Identified, OID)
- private String place; // 身份证地址
- // 身份证与用户,一对一的关系
- private User user;</span>
- <span style="font-family:KaiTi_GB2312;">// 用户
- public class User {
- private int userId;
- private String userName;
- // 用户与身份证信息, 一对一关系
- private IdCard idCard;</span>
IdCard.hbm.xml有外键方:
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.c_one2one">
- <class name="IdCard" table="t_IdCard">
- <id name="cardNum">
- <generator class="assigned"></generator>
- </id>
- <property name="place" length="20"></property>
- <!--
- 一对一映射,有外键方
- unique="true" 给外键字段添加唯一约束
- -->
- <many-to-one name="user" unique="true" column="user_id" class="User" cascade="save-update"></many-to-one>
- </class>
- </hibernate-mapping></span>
User.hbm.xml无外键方
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.c_one2one">
- <class name="User" table="t_user">
- <id name="userId">
- <generator class="native"></generator>
- </id>
- <property name="userName" length="20"></property>
- <!--
- 一对一映射: 没有外键方
- -->
- <one-to-one name="idCard" class="IdCard"></one-to-one>
- </class>
- </hibernate-mapping>
- </span>
三、基于主键的映射
- <span style="font-family:KaiTi_GB2312;">// 身份证
- public class IdCard {
- private int user_id;
- // 身份证号
- private String cardNum;
- private String place; // 身份证地址
- // 身份证与用户,一对一的关系
- private User user;</span>
IdCard.hbm.xml
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.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="cardNum" length="20"></property>
- <property name="place" length="20"></property>
- <!--
- 一对一映射,有外键方
- (基于主键的映射)
- constrained="true" 指定在主键上添加外键约束
- -->
- <one-to-one name="user" class="User" constrained="true" cascade="save-update"></one-to-one>
- </class>
- </hibernate-mapping>
- </span>
User.hbm.xml
- <span style="font-family:KaiTi_GB2312;"><?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.lfsenior.c_one2one2">
- <class name="User" table="t_user">
- <id name="userId">
- <generator class="native"></generator>
- </id>
- <property name="userName" length="20"></property>
- <!--
- 一对一映射: 没有外键方
- -->
- <one-to-one name="idCard" class="IdCard"></one-to-one>
- </class>
- </hibernate-mapping>
- </span>