hibernate 一对一主键映射关系
2018-03-02 16:27
609 查看
User类:package com.wgh.model;
/**
* 用户持久化类
*/
public class User {
private Integer id; //id编号
private String username; //姓名
private Integer age; //年龄
private IdCard idCard; //证件
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
IdCard类:
![](https://img-blog.csdn.net/2018030216232016?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjgwNjE0ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
tb_idCard_onetoone表:
/**
* 用户持久化类
*/
public class User {
private Integer id; //id编号
private String username; //姓名
private Integer age; //年龄
private IdCard idCard; //证件
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
IdCard类:
package com.wgh.model; /** * 证件持久化类 */ public class IdCard { private Integer id; //id private String num; //号码 private User user; //用户 public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } }User(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.wgh.model"> <class name="com.wgh.model.User" table="tb_user_onetoone"> <!-- 主键id --> <id name="id"> <generator class="native"/> </id> <!-- 姓名 --> <property name="username" not-null="true" /> <!-- 年龄 --> <property name="age"/> <!-- 一对一映射 --> <one-to-one name="idCard"/> </class> </hibernate-mapping>IdCard(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.wgh.model"> <class name="com.wgh.model.IdCard" table="tb_idCard_onetoone"> <!-- 主键id --> <id name="id"> <!-- 参考User的外键 --> <generator class="foreign"> <param name="property">user</param> </generator> </id> <!-- 证件号 --> <property name="num" not-null="true"/> <!-- 一对一映射 --> <one-to-one name="user" constrained="true"/> </class> </hibernate-mapping>tb_user_onetoone表:
tb_idCard_onetoone表:
相关文章推荐
- Hibernate关系映射 --- 一对一实例分析(双向关联,是基于主键的)
- Hibernate实体关系映射——主键相同的一对一关系
- hibernate 关系映射之 主键关联一对一
- hibernate笔记--基于主键的单(双)向的一对一映射关系
- Hibernate关系映射级别注解(一对一双向外键关联、一对一双向外键联合主键)
- Hibernate关系映射(3)_一对一单向主键关联
- Hibernate之对象关系映射02一对一单向主键关联
- Hibernate关系映射(4)_一对一双向主键关联
- Hibernate关系映射(5)_一对一单向联合主键关联
- Hibernate基于主键的一对一映射关系
- Hibernate实体关系映射:单向主键一对一关联
- Hibernate之对象关系映射05一对一双向主键关联
- hibernate实现一对一关系映射(基于主键)
- Hibernate实体关系映射:双向主键一对一关联
- Hibernate实体关系映射——主键相同的一对一关系
- [原创]java WEB学习笔记85:Hibernate学习之路-- -映射 一对一关系 ,基于主键方式实现
- 第九章 关系映射 一对一关系 共享主键方式实现一对一
- hibernate实现非主键映射外键双向多对一关系
- Hibernate一对一 主键关联映射(one-to-one)
- hibernate关系映射 一对一