Hibernate5.2之一对一外键关联(五)

一.简介

  上篇文章中笔者介绍了Hibernate关联关系中的一对一外键关联,本篇博客将介绍一对一外键关联。其实我们回过头想一想,外键关联其实就是一对多关联关系中将多的一方简化为一个,就是我们本文所要介绍的一对一的外键关联。

二.外键关联

2.1数据库表的创建

create table people (
id varchar2(255 char) not null,
name varchar2(255 char),
sex varchar2(255 char),
primary key (id)
); create table cards (
id varchar2(255 char) not null,
card_num varchar2(255 char),
people_id varchar2(255 char),
primary key (id)
);

2.2 hbm文件的方式

public class People {
private String id;
private String name;
private String sex;
private IdCard idCard; //setter and getter
} public class IdCard {
private String id;
private String cardNum;
private People people; //setter and getter
}

People.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.demo.hibernate.one2one.People" table="people">
<id name="id" type="string">
<generator class="uuid"></generator>
</id> <property name="name" type="string" column="name"></property>
<property name="sex" type="string" column="sex"></property> <one-to-one name="idCard" class="com.demo.hibernate.one2one.IdCard" cascade="all" property-ref="people"></one-to-one>
</class>
</hibernate-mapping>

IdCard.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.demo.hibernate.one2one.IdCard" table="cards">
<id name="id" type="string" column="id">
<generator class="uuid"></generator>
</id> <property name="cardNum" type="string" column="card_num"></property>
<many-to-one name="people" class="com.demo.hibernate.one2one.People" column="people_id" unique="true"></many-to-one>
</class>
</hibernate-mapping>

2.3 注解的方式

People.java

@Entity
@Table(name="people")
public class People { @Id
@Column(name="id")
@GenericGenerator(name="uuidGenerator", strategy="uuid")
@GeneratedValue(generator="uuidGenerator")
private String id; @Column(name="name")
private String name; @Column(name="sex")
private String sex; @OneToOne(cascade={CascadeType.ALL}, fetch=FetchType.LAZY, mappedBy="people")
private IdCard idCard; //setter and getter
}

IdCard.java

@Entity
@Table(name="cards")
public class IdCard { @Id
@Column(name="id")
@GenericGenerator(name="uuidGenerator", strategy="uuid")
@GeneratedValue(generator="uuidGenerator")
private String id; @Column(name="card_num")
private String cardNum; @ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="people_id")
private People people; //setter and getter
}

2.4 代码测试

A.保存

@Test
public void save(){
Transaction tx = session.beginTransaction();
People people = new People();
people.setName("AAA");
people.setSex("男"); IdCard idCard = new IdCard();
idCard.setCardNum("889900");
idCard.setPeople(people); people.setIdCard(idCard);
session.save(people); tx.commit();
}

B.get

@Test
public void get(){
People people = session.get(People.class, "402882e6564a70fa01564a70fbd40000");
System.out.println("此时已经发送了SQL语句");
System.out.println(people.getName() + "::" + people.getSex());
IdCard idCard = people.getIdCard();
System.out.println(idCard.getCardNum());
}

C.load

@Test
public void load(){
People people = session.load(People.class, "402882e6564a70fa01564a70fbd40000");
System.out.println("此时没有发送任何的SQL语句");
System.out.println(people.getName() + "::" + people.getSex());
System.out.println("=============");
IdCard idCard = people.getIdCard();
System.out.println(idCard.getCardNum());
}

D.delete

@Test
public void delete(){
People people = new People();
people.setId("402882e6564a7c1501564a7c16d80000"); IdCard card = new IdCard();
card.setId("402882e6564a7c1501564a7c16e50001"); people.setIdCard(card);
Transaction tx = session.beginTransaction();
session.delete(people);
tx.commit();
}

E.update

@Test
public void update(){
Transaction tx = session.beginTransaction();
People people = new People();
people.setId("402882e6564a70fa01564a70fbd40000");
people.setName("YYYY");
people.setSex("男");
session.update(people);
tx.commit();
}

最新文章

  1. PEM (Privacy Enhanced Mail) Encoding
  2. WPF MVVM模式
  3. 为mysql在表的某一位置增加一列
  4. 纯CSS最小响应网格布局
  5. LeetCode15 3Sum
  6. bzoj1150
  7. 数据库连接字符串ConnectionString 中的关键字值释义
  8. &lt;&lt;深入Java虚拟机&gt;&gt;-第二章-Java内存区域-学习笔记
  9. js中with、this的用法
  10. 矩形类定义【C++】
  11. [转]centos7 安装jdk11 并设置默认java版本
  12. visual studio 阅读 linux-kernel
  13. Python关键字及其用法
  14. MSSQL DB Replication Error
  15. 使用GetAdaptersInfo时,网卡类型的值为71
  16. P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】
  17. java构造代码块与静态代码块
  18. Thread(26)
  19. mac下 配置homebrew 和java home
  20. Maximum Shortest Distance 最大团 二分答案 HDU 3585

热门文章

  1. 用Unity代码通过Xml配置生成GameObject之——前两天掉的坑
  2. A Simple Problem with Integers_树状数组
  3. 探究toString()和valueOf()
  4. Scrollview嵌套Listview运行后最先显示出来的位置不在顶部而是中间问题
  5. display:inline; display:block;
  6. linux压缩解压文件
  7. JS对象实现随机满天小星星实例
  8. ActiveMQ的初夜
  9. ios常见的页面传值方式
  10. C#中的选择语句