Hibernate学习(三)---【一对一主键双向关联】
Person和IdCard一对一双向关联Person.java
package
com.zzn.hibernate.OneToOne;
public class Person {
private int id;
private String name;
private int age;
// 双向关联需要加上Idcard的属性
private IdCard idCard;
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this .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 int getAge() {
return age;
}
public void setAge( int age) {
this .age = age;
}
}
public class Person {
private int id;
private String name;
private int age;
// 双向关联需要加上Idcard的属性
private IdCard idCard;
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this .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 int getAge() {
return age;
}
public void setAge( int age) {
this .age = age;
}
}
IdCard.java
package
com.zzn.hibernate.OneToOne;
public class IdCard {
private int id;
private String cardNo;
// 双向关联需要加上person的属性;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this .person = person;
}
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this .cardNo = cardNo;
}
}
public class IdCard {
private int id;
private String cardNo;
// 双向关联需要加上person的属性;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this .person = person;
}
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this .cardNo = cardNo;
}
}
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 >
< class name ="com.zzn.hibernate.OneToOne.Person" table ="PERSON" >
< id name ="id" column ="id" >
< generator class ="foreign" >
< param name ="property" > idCard </ param >
</ generator >
</ id >
< property name ="name" />
< property name ="age" />
< one-to-one name ="idCard" constrained ="true" />
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name ="com.zzn.hibernate.OneToOne.Person" table ="PERSON" >
< id name ="id" column ="id" >
< generator class ="foreign" >
< param name ="property" > idCard </ param >
</ generator >
</ id >
< property name ="name" />
< property name ="age" />
< one-to-one name ="idCard" constrained ="true" />
</ class >
</ hibernate-mapping >
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 >
< class name ="com.zzn.hibernate.OneToOne.IdCard" table ="ID_CARD" >
< id name ="id" column ="id" >
< generator class ="native" />
</ id >
< property name ="cardNo" />
< one-to-one name ="person" />
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name ="com.zzn.hibernate.OneToOne.IdCard" table ="ID_CARD" >
< id name ="id" column ="id" >
< generator class ="native" />
</ id >
< property name ="cardNo" />
< one-to-one name ="person" />
</ class >
</ hibernate-mapping >
test
package
com.zzn.hibernate;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zzn.hibernate.OneToOne.IdCard;
import com.zzn.hibernate.OneToOne.Person;
import com.zzn.hibernate.hibernateUtil.HibernateUtils;
public class OneToOne_pk2 extends TestCase {
public void OneToOne_pk2_1() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
IdCard idCard = new IdCard();
Person person = new Person();
idCard.setCardNo("11111111");
person.setName("刘德华");
person.setAge(50);
person.setIdCard(idCard);
System.out.println("person's name = " + person.getName());
System.out.println("idCard's cardNo = "
+ person.getIdCard().getCardNo());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
if (session.isOpen()) {
tx.commit();
session.close();
}
}
}
}
public void OneToOne_pk2_2() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
IdCard idCard = new IdCard();
Person person = new Person();
person.setName("周杰伦");
person.setAge(30);
idCard.setCardNo("2222222");
idCard.setPerson(person);
System.out.println("idCard's number = " + idCard.getCardNo());
System.out.println("person's name = "
+ idCard.getPerson().getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
tx.commit();
HibernateUtils.closeSession(session);
}
}
}
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zzn.hibernate.OneToOne.IdCard;
import com.zzn.hibernate.OneToOne.Person;
import com.zzn.hibernate.hibernateUtil.HibernateUtils;
public class OneToOne_pk2 extends TestCase {
public void OneToOne_pk2_1() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
IdCard idCard = new IdCard();
Person person = new Person();
idCard.setCardNo("11111111");
person.setName("刘德华");
person.setAge(50);
person.setIdCard(idCard);
System.out.println("person's name = " + person.getName());
System.out.println("idCard's cardNo = "
+ person.getIdCard().getCardNo());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
if (session.isOpen()) {
tx.commit();
session.close();
}
}
}
}
public void OneToOne_pk2_2() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
IdCard idCard = new IdCard();
Person person = new Person();
person.setName("周杰伦");
person.setAge(30);
idCard.setCardNo("2222222");
idCard.setPerson(person);
System.out.println("idCard's number = " + idCard.getCardNo());
System.out.println("person's name = "
+ idCard.getPerson().getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
tx.commit();
HibernateUtils.closeSession(session);
}
}
}