例子:人与身份证的一对一
人实体类
package entity;
/*
* Person 实体类
* */
public class Person {
private int id;//编号
private String name;//姓名
private IdCard idCard;
public Person() {
}
public Person(String name) {
this.name = name;
}
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 IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
人实体类的XML配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Person">
<id name="id">
<generator class="sequence">
<param name="sequence">seq_person</param>
</generator>
</id>
<property name="name" />
<one-to-one name="idCard" class="entity.IdCard" />
</class>
</hibernate-mapping>
身份证实体类
package entity;
/*
* Person 实体类
* */
public class IdCard {
private int id;//编号
private String name;//姓名
private Person person;
public IdCard(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public IdCard() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
身份证实体类的XML配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.IdCard">
<id name="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="name" />
<one-to-one name="person" class="entity.Person" constrained="true"/>
</class>
</hibernate-mapping>
使用hibernate.cfg.xml映射其的XML路径
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库URL -->
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:oracle11
</property>
<!-- 数据库用户 -->
<property name="connection.username">A_hr</property>
<!-- 数据库用户密码 -->
<property name="connection.password">123456</property>
<!-- 数据库 JDBC 驱动 -->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 是否将运行期生成的 SQL 输出到日志以供调试 -->
<property name="show_sql">true</property>
<!-- 每个数据库都有其对应的 Dialect 以匹配其平台特征 -->
<property name="dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="entity/IdCard.hbm.xml" />
<mapping resource="entity/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
进行数据的操作
package test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import entity.IdCard;
import entity.Person;
import util.HibernateSessionFactory;
public class OneToOneTest {
public static void main(String[] args) {
oneToOne();
}
private static void oneToOne() {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
Person person = new Person("李炎恢");
IdCard idCard = new IdCard("123456");
try {
person.setIdCard(idCard);
idCard.setPerson(person);
tx = session.beginTransaction();
session.save(person);
session.save(idCard);
tx.commit();
System.out.println("保存成功!!!");
} catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
}
效果图:
在数据库查询这两条语句
select id, name from person;
select id, name from idcard