【7.4.1】基于外键的双向一对一的关联映射
1.公民表person 和身份证 IdCard
2.类Person 和IdCard类
public class Person {
private int id;
private String name;
private int age;
private IdCard idCard;
//get…set
}
public class IdCard{
private int id;
private String code;
private Person person;
//get…set
}
3.配置文件
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="cn.siggy.pojo"> <class name="IdCard"> <id name="id"> <generator class="native"></generator> </id> <property name="code"/> <one-to-one name="person" property-ref="idCard"/> </class> </hibernate-mapping>
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="cn.siggy.pojo"> <class name="Person"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <property name="age"/> <!-- unique="true"时候 可以设置一对一的关系 --> <many-to-one name="idCard" class="IdCard" cascade="save-update" column="idcard_id" foreign-key="fk_idcard" not-null="true" unique="true"/> </class> </hibernate-mapping>
4.测试代码【由上至下依次单元测试@Test】
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否生成ddl脚本 第二个参数 是否执行到数据库中
se.create(true, true);
}
@Test
public void testSave() throws HibernateException, SerialException, SQLException{
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
tx = session.beginTransaction();
IdCard id1 = new IdCard();
id1.setCode("320123231212112");
IdCard id2 = new IdCard();
id2.setCode("110231432423432");
Person person1 = new Person();
person1.setName("贾宝玉");
person1.setAge(23);
person1.setIdCard(id1);
Person person2 = new Person();
person2.setName("林黛玉");
person2.setAge(22);
person2.setIdCard(id2);
Person person3 = new Person();
person3.setName("薛宝钗");
person3.setAge(21);
person3.setIdCard(id2);
session.save(person1);
session.save(person2);
//当保存person3出错
//session.save(person3);
tx.commit();
}catch (HibernateException e) {
if(tx!=null)
tx.rollback();
e.printStackTrace();
throw e;
}finally{
HibernateUtil.closeSession();
}
}
@Test
public void testGet(){
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Person person = (Person)session.get(Person.class, 1);
System.out.println("personName="+person.getName()+"---IdCard:"+person.getIdCard().getCode());
System.out.println("=======================================");
IdCard idCard = (IdCard)session.get(IdCard.class, 2);
System.out.println("personName="+idCard.getPerson().getName()+"--Idcard:"+idCard.getCode());
//取数据
tx.commit();
}catch (HibernateException e) {
if(tx!=null)
tx.rollback();
e.printStackTrace();
throw e;
}finally{
HibernateUtil.closeSession();
}
}
}
5.测试结果
1.运行testSave()方法:
2.运行testGet()方法:





浙公网安备 33010602011771号