Person.java
package cn.itcast.hibernate.domain;
public class Person {
private int id;
private String name;
private IdCard idCard;
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;
}
}
Person.hbm.xml
<?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="cn.itcast.hibernate.domain">
<class name="Person">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<one-to-one name="idCard"></one-to-one>
</class>
</hibernate-mapping>
IdCard.java
package cn.itcast.hibernate.domain;
import java.util.Date;
public class IdCard {
private int id;
private Date usefulLife;
private Person person;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getUsefulLife() {
return usefulLife;
}
public void setUsefulLife(Date usefulLife) {
this.usefulLife = usefulLife;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
IdCard.hbm.xml
<?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="cn.itcast.hibernate.domain">
<class name="IdCard" table="id_card" >
<id name="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="usefulLife" column="useful_life"/>
<one-to-one name="person" constrained="true"/>
</class>
</hibernate-mapping>
One2One.java
package cn.itcast.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.itcast.hibernate.domain.IdCard;
import cn.itcast.hibernate.domain.Person;
public class One2One {
public static void main(String[] args) {
add();
query(1);
}
// 查询
static void query(int personId) {
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtil.getSession();
transaction = session.beginTransaction();
//查询主对象时候执行一次SQL语句
/*Person person=(Person) session.get(Person.class, personId);
System.out.println(person.getIdCard().getUsefulLife());*/
System.out.println("****************************************************************");
//查询从对象的时候执行的2次SQL语句
IdCard idCard=(IdCard) session.get(IdCard.class, personId);
System.out.println(idCard.getPerson().getName());
transaction.commit();
} finally {
if (session != null)
session.close();
}
}
// 添加
static Person add() {
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtil.getSession();
transaction = session.beginTransaction();
IdCard idCard = new IdCard();
idCard.setUsefulLife(new Date());
// 在这里,person是主对象,IdCard是从对象
// IdCard的主键既是主键,也是外键
Person person = new Person();
person.setName("p1");
person.setIdCard(idCard);
idCard.setPerson(person);
session.save(person);
session.save(idCard);
transaction.commit();
return person;
} finally {
if (session != null)
session.close();
}
}
}
sql
Hibernate:
insert
into
Person
(name)
values
(?)
Hibernate:
insert
into
id_card
(useful_life, id)
values
(?, ?)
****************************************************************
Hibernate:
select
idcard0_.id as id3_0_,
idcard0_.useful_life as useful2_3_0_
from
id_card idcard0_
where
idcard0_.id=?
Hibernate:
select
person0_.id as id4_1_,
person0_.name as name4_1_,
idcard1_.id as id3_0_,
idcard1_.useful_life as useful2_3_0_
from
Person person0_
left outer join
id_card idcard1_
on person0_.id=idcard1_.id
where
person0_.id=?
p1