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(); }