一对一关系
表与表之间互相对应,各式互相的唯一
常用的例子是:身份证和人之间的关系,或是工作证与个人的关系,我这里举例老师和工作证的关系。
teachers表
字段名 |
类型 |
约束 |
id |
int |
主键 |
t_name |
varchar |
|
|
card表
字段名 |
类型 |
约束 |
card_num |
varchar |
|
t_id |
int |
主键/外键 |
Teachers.java
package com.ben.pojo;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Teachers implements Serializable {
private Integer id;
private String tName;
//card对象
private Card card;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String gettName() {
return tName;
}
public void settName(String tName) {
this.tName = tName;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public Teachers() {
super();
}
public Teachers(String tName) {
super();
this.tName = tName;
}
}
Card.java
注意:一个tId设置为主键;一个teacher设置为外键的对象。
即——主键表因为主外键都是一个字段,所以在域对象设定的时候要设置主键和外键。
package com.ben.pojo;
import java.io.Serializable;
public class Card implements Serializable {
private Integer tId;
private Teachers teacher;
private String cardNum;
public Integer gettId() {
return tId;
}
public void settId(Integer tId) {
this.tId = tId;
}
public Teachers getTeacher() {
return teacher;
}
public void setTeacher(Teachers teacher) {
this.teacher = teacher;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
public Card() {
super();
}
public Card(Teachers teacher, String cardNum) {
super();
this.teacher = teacher;
this.cardNum = cardNum;
}
}
Teachers.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.ben.pojo.Teachers" table="teachers">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="tName" type="java.lang.String">
<column name="t_name"></column>
</property>
<!-- <set name="grades" cascade="save-update">
<key column="gid"></key>
<many-to-many></many-to-many>
</set> -->
<!-- 一对一,外键表配置级联关系 -->
<one-to-one name="card" class="com.ben.pojo.Card" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
Card.hbm.xml
注意:这里主键的生成方式为'foreign'。
<param name="property"></param>中间的参数与下文配置的name相同
<?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.ben.pojo.Card" table="card">
<id name="tId" column="t_id" type="java.lang.Integer">
<generator class="foreign">
<param name="property">teacher</param>
</generator>
</id>
<property name="cardNum" type="java.lang.String">
<column name="card_num"></column>
</property>
<!-- 一对一 -->
<one-to-one name="teacher" class="com.ben.pojo.Teachers"></one-to-one>
</class>
</hibernate-mapping>
测试用例
@Test
public void test() {
Session session = MySessionFactory.getSession();
Transaction tx = session.beginTransaction();
Teachers t = new Teachers("laoshi2");
Card c = new Card(t, "11223344556688");
t.setCard(c);
session.save(t);
tx.commit();
}
注意,在创建Teachers对象后,不要忘记设置Card,否则保存Teachers对象会不保存Card的。但是这时保存Card会保存Teachers对象,自己要理好关系。
Hibernate:
insert
into
teachers
(t_name)
values
(?)
Hibernate:
insert
into
card
(card_num, t_id)
values
(?, ?)
网友评论