hiberbate的一对一关联关系

接着昨天的,今天我们来看一下一对一关联关系。我个人觉得这个关系最简单,理解起来也比较容易,在现实生活中这样的例子很多:如人和身份证,学生和学生证等。下面我来写一个人和身份证的例子。这个例子我做了简化,属性也比较少。开发环境我就不多说了,下面就一步一步的来写一下这个例子。

1.写2个vo类

package org.lxh.vo;

public class Person {
	private int id;
	private String name;
    private Card card;
	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 Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

}


package org.lxh.vo;

public class Card {
	private int id;
	private String birthday;
	private Person person;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	
}


在这里一个人对应一个身份证也可以说一个身份证只属于某个人,这里使用单项关联和双向关联都可以,我个人建议使用单项关联。

2.写2个hibernate映射文件,代码如下。

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.annotations">

    <class name="org.lxh.vo.Person" table="person">

        <id name="id" column="id">
            <generator class="increment"/>
        </id>

        <property name="name" column="name"/>
        <!-- <one-to-one name="card"/> --> <!-- 2个都写就表示双向关联 -->
        <!-- <many-to-one name="card" unique="true"/> --> <!-- 多对一的特殊情况 -->
        
    </class> 

</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.annotations">

    <class name="org.lxh.vo.Card" table="card">

        <id name="id" column="id">
            <generator class="foreign">
              <param name="property">person</param> <!-- 这里的id是外键主键表就是person -->
            </generator>
        </id>

        <property name="birthday" column="birthday"/>
        <one-to-one name="person"/>
        
    </class> 

</hibernate-mapping>


3.最后来写一个Junit4测试一下所写的代码,至于查询的代码,我就不写出来了大家可以自己写,下面的代码段含有昨天的测试部分,大家看清楚啊。

package org.lxh.test;


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.BeforeClass;
import org.lxh.vo.Article;
import org.lxh.vo.Author;
import org.lxh.vo.Card;
import org.lxh.vo.Person;

import util.HibernateUtil;

public class Test {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
    @org.junit.Test public void save(){
    	Session ses=null;
    	
    	Transaction tr=null;
    	try{
    		ses=HibernateUtil.getSession();
    		tr=ses.beginTransaction();
    		Author author=new Author();
    		author.setName("chenwill4");
    		Article article=new Article();
    		article.setAuthor(author);  //建立2个对象的关联
    		article.setName("老鹰抓小鸡3");
    		ses.save(author);  //将author信息保存
    		ses.save(article);
    		tr.commit();      //将事务提交,否侧数据不会被保存
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally{
    		if(ses!=null){
    			ses.close();  //关闭连接,否则会把数据库卡死
    		}
    	}
    	
    }
    @org.junit.Test public void queryByAuthor(){
    	Session ses=null;
    	
    	Transaction tr=null;
    	try{
    		ses=HibernateUtil.getSession();
    		tr=ses.beginTransaction();
    		Article art=(Article)ses.get(Article.class, 3);
    		
    		
    		System.out.println("作者的姓名是:"+art.getAuthor().getName());  //通过文章查询作者
    		
    		
    		tr.commit();      //将事务提交
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally{
    		if(ses!=null){
    			ses.close();  //关闭连接,否则会把数据库卡死
    		}
    	}
    	
    }
    
    @org.junit.Test public void insert121(){
    	Session ses=null;
    	
    	Transaction tr=null;
    	try{
    		ses=HibernateUtil.getSession();
    		tr=ses.beginTransaction();
    		Card card=new Card();
    		Person p=new Person();
    		p.setCard(card);
    		p.setName("陈瑞银");
    		
    		
    		card.setBirthday("2009-02-12");
    		card.setPerson(p);
    		ses.save(card);
    		ses.save(p);
    		tr.commit();      //将事务提交
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally{
    		if(ses!=null){
    			ses.close();  //关闭连接,否则会把数据库卡死
    		}
    	}
    	
    }
}


ok程序写好了,来看一下效果截图,如下图所示,

好了,到这里程序就结束了,源代码在我的资源里免积分下载,需要的可以自己弄,有问题可以和我交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值