IdCard的主键不是自增长,是用Person的Id作为主键同时作为外键,IdCard的主键随Person的Id的变化而改变,其实两个主键的值是相等的。
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;
}
}
IdCard
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 Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Date getUsefullife() {
return usefullife;
}
public void setUsefullife(Date usefullife) {
this.usefullife = usefullife;
}
}
JavaBean映射文件1(Person.hbm.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 package="cn.itcast.hibernate.domain">
<class name="Person" table="person">
<id name="id">
<generator class="native" />
</id>
<property name="name" unique="true"/>
<one-to-one name="idcard"/>
</class>
</hibernate-mapping>
JavaBean映射文件2(IdCard.hbm.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 package="cn.itcast.hibernate.domain">
<class name="IdCard" table="id_card">
<id name="id">
<generator class="foreign" >
<!-- IdCard的id根据这个属性找到person的getId()获得id值 -->
<param name="property">person</param>
</generator>
</id>
<property name="usefullife" column="useful_life"/>
<one-to-one name="person" constrained="true"/>
</class>
</hibernate-mapping>
Hibernate配置文件(hibernate.cfg.xml)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup如果没有表则自动创建 -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="cn/itcast/hibernate/domain/Person.hbm.xml" />
<mapping resource="cn/itcast/hibernate/domain/IdCard.hbm.xml" />
</session-factory>
</hibernate-configuration>
One2One.java用来使用映射
package cn.itcast.hibernate;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.itcast.hibernate.domain.Department;
import cn.itcast.hibernate.domain.Employee;
import cn.itcast.hibernate.domain.IdCard;
import cn.itcast.hibernate.domain.Person;
public class One2One {
/**
* @param args
*/
public static void main(String[] args) {
add();
queryPerson(1);
// queryIdCard(1);
}
static Person queryPerson(int id){
Session s= null;
Transaction tx = null;
try {
s=HibernateUtil.getSession();
tx= s.beginTransaction();
Person person=(Person)s.get(Person.class, id);
System.out.println(person.getIdcard().getUsefullife());
tx.commit();
return person;
}finally{
if(s!=null){
s.close();
}
}
}
static IdCard queryIdCard(int id){
Session s= null;
Transaction tx = null;
try {
s=HibernateUtil.getSession();
tx= s.beginTransaction();
IdCard idCard=(IdCard)s.get(IdCard.class, id);
System.out.println(idCard.getPerson().getName());
tx.commit();
return idCard;
}finally{
if(s!=null){
s.close();
}
}
}
static Person add(){
Session s= null;
Transaction tx = null;
try {
s=HibernateUtil.getSession();
tx= s.beginTransaction();
IdCard idcard = new IdCard();
idcard.setUsefullife(new Date());
Person person = new Person();
person.setName("personname1");
person.setIdcard(idcard);
idcard.setPerson(person);
s.save(person);
s.save(idcard);
tx.commit();
return person;
}finally{
if(s!=null){
s.close();
}
}
}
}