类图:
person表中设置一个外键card_id 参考card表的主键。
Person.java类
package com.org.model;
public class Person {
private int id;
private String name;
private Card card;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
}
Card.java类
package com.org.model;
public class Card {
private int id;
private String cardNum;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
}
Person.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.org.model"> <class name="Person" table="person"> <id name="id"> <generator class="native"/> </id> <property name="name" column="name" not-null="true"/> <!-- name指Student类中对应一方的属性名 cascade属性指定级联操作 unique指定外键的唯一性 class指一方的类名 column表示关联的外键 not-null表示此外键不能为空 --> <many-to-one name="card" class="Card" cascade="save-update" unique="true" column="card_id" not-null="true"> </many-to-one> </class> </hibernate-mapping>
Card.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.org.model"> <class name="Card" table="card"> <id name="id"> <generator class="native"/> </id> <property name="cardNum" column="card_num" not-null="true"/> </class> </hibernate-mapping>
hibernate.cfg.xml配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hbn</property>
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<!--<mapping resource="com/org/model/MyClass.hbm.xml" />
<mapping resource="com/org/model/Student.hbm.xml" />
-->
<mapping resource="com/org/model/Person.hbm.xml" />
<mapping resource="com/org/model/Card.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试类:
package com.org.model.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.org.model.Card;
import com.org.model.Person;
import com.org.util.HibernateUtil;
public class HibernateTest1 {
@Test
public void testSave1() {
Session session = null;
Transaction tx = null;
try {
// 拿到session
session = HibernateUtil.getSession();
// 开启事务
tx = session.beginTransaction();
// 给实体赋值
Person person = new Person();
person.setName("aaa");
Card card = new Card();
card.setCardNum("11111111111111");
person.setCard(card);
session.save(person);
//提交事务
tx.commit();
} catch (Exception e) {
// 打印堆栈信息
e.printStackTrace();
// 事务回滚
if (tx != null) {
tx.rollback();
}
} finally {
HibernateUtil.closeSession(session);
}
}
}
结果:
Hibernate: insert into card (card_num) values (?)
Hibernate: insert into person (name, card_id) values (?, ?)
一对一外键关联其实就是特殊的单向多对一关联。在多方配置unique=“true”