第一种情况:IdCard类的ID 既是主键又是外键
package dao.po;
//人
public class Person {
private int id; //ID
private String name;//姓名
private IdCard idCard; //身份证(对象)
}
package dao.po;
//身份主类
public class IdCard{
private int id;//ID
private String address;//地址
private Person person; //身份证的主人
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="dao.po">
<class name="Person">
<id name="id">
<generator class="native"/>
</id>
<property name="name" not-null="true" length="255" column="`name`"/>
<!-- 这里做一对一映射 -->
<!-- 没什么好说的,就是一 one-to-one -->
<one-to-one name="idCard" ></one-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="dao.po">
<class name="IdCard" table="id_card">
<id name="id">
<!-- 注意: -->
<!-- 本类的id 既是主键,又是外键 -->
<!-- IdCard对象的是从对象, Person是主对象, 先有主,后有从. -->
<generator class="foreign">
<!-- 此处的person是IdCard类的一个属性 -->
<param name="property">person</param>
</generator>
</id>
<property name="address" not-null="true" length="255" column="`address`"/>
<!-- 此处的person是IdCard类的一个属性 -->
<!-- constrained="true" 对生成的数据表产生约束,id_card表的id既是主键,又是外键 -->
<!-- constrained="false" 表结构上没有约束, 取何值对对象关系没影响,只是对表结构有影响-->
<one-to-one name="person" constrained="true"/>
</class>
</hibernate-mapping>
package dao;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import dao.po.IdCard;
import dao.po.Person;
public class One2One{
public static void main(final String[] args){
add();
final IdCard c1 = query(5);
System.out.println(c1.getPerson().getName());
}
//保存
public static void add(){
final Person p1 = new Person();
p1.setName("小明");
final IdCard c1 = new IdCard();
c1.setAddress("北京市海淀区上地");
p1.setIdCard(c1); //建立关联(不能少)
c1.setPerson(p1); //建立关联(不能少)
Session session = null;
try{
session = HibernateUtil.getSeesion();
final Transaction tx = session.beginTransaction();
//session.save(p1); //
session.save(c1); //身份证是从对象依赖于主对象, "身份证"依赖于"人",
//保存 c1 , 也会自动保存 他依赖的p1,前题是c1 与p1要关联
tx.commit();
//SQL 如下:
//Hibernate: insert into Person (`name`) values (?)
//Hibernate: insert into id_card (`address`, id) values (?, ?)
//虽然只执行了session.save(c1) , 但有两个insert
}finally{
if (session != null){
session.close();
}
}
}
//查询 身份证
public static IdCard query(final int id){
Session session = null;
try{
session = HibernateUtil.getSeesion();
final IdCard c1 = (IdCard) session.get(IdCard.class, id);
Hibernate.initialize(c1.getPerson());
return c1;
//SQL 如下:
//Hibernate: select idcard0_.id as id3_0_, idcard0_.`address` as address2_3_0_ from id_card idcard0_ where idcard0_.id=?
//Hibernate: select person0_.id as id2_1_, person0_.`name` as name2_2_1_, idcard1_.id as id3_0_, idcard1_.`address` as address2_3_0_ from Person person0_ left outer join id_card idcard1_ on person0_.id=idcard1_.id where person0_.id=?
//注意person表又连了id-card表
}finally{
if (session != null){
session.close();
}
}
}
}
第二种情况:IdCard类的ID 只是主键, IdCard类多对一属性 person_id做 外键,指向Person类的主键。
这种情况有点像 多对一,关系如下图:
两个配置文件内容如下