标签:hibernate
一对一映射一共有两种映射方式:
使用用户和身份证信息的一对一的关系
User:
package cn.itcast.one2one; public class User { private int userId;//用户id 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; } }IdCard:
package cn.itcast.one2one; public class IdCard { private int user_id;// private int cardNum;//身份证号码 private String place;//出生地 private User user; public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public int getCardNum() { return cardNum; } public void setCardNum(int 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; } }
<?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.itcast.one2one" auto-import="true"> <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" cascade="save-update"></one-to-one> </class> </hibernate-mapping>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="cn.itcast.one2one" auto-import="true"> <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>
<?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.itcast.one2one" auto-import="true"> <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" cascade="save-update"></one-to-one> </class> </hibernate-mapping>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="cn.itcast.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>
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:hibernate
原文地址:http://blog.csdn.net/u014010769/article/details/47114751