想飞就别怕摔

大爷的并TM骂人

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;
    }
    
}

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;
    }
    
}

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>

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>

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);
        }

    }

}

posted on 2009-11-01 12:08 生命的绽放 阅读(437) 评论(0)  编辑  收藏 所属分类: Hibernate


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年11月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜