频道栏目
首页 > 程序开发 > 软件开发 > 其他 > 正文
006——hibernate一对一关联映射
2016-07-18 10:27:25         来源:msj_god_boy的博客  
收藏   我要投稿

一对一关联映射有两种方式:主键关联和外键关联

以夫妻为例(Husband----Wife)

单向关联实体类:

Husband.java

package com.java.hibernate;

public class Husband {
	
	private int id;
	
	private String name;
	
	private Wife wife;

	public Wife getWife() {
		return wife;
	}

	public void setWife(Wife wife) {
		this.wife = wife;
	}

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

 

Wife.java

 

package com.java.hibernate;

public class Wife {

	private int id;
	
	private String name;

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

 

双向关联实体类:

Husband.java

 

package com.java.hibernate;

public class Husband {
	
	private int id;
	
	private String name;
	
	private Wife wife;

	public Wife getWife() {
		return wife;
	}

	public void setWife(Wife wife) {
		this.wife = wife;
	}

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

 

Wife.java

 

package com.java.hibernate;

public class Wife {

	private int id;
	
	private String name;
	
	private Husband husband;

	public Husband getHusband() {
		return husband;
	}

	public void setHusband(Husband husband) {
		this.husband = husband;
	}

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

 

单向主键关联:

Husband映射文件:

 




	
		
			
			
				wife
					
		
		
		
		
	
Wife映射文件:


	

	
		
			
		
		
	
测试类
package com.java.hibernate;

import junit.framework.TestCase;

import org.hibernate.Session;

public class Test extends TestCase{
	
	public void testSave1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Wife wife = new Wife();
			wife.setName("昆凌");
			
			Husband husband = new Husband();
			husband.setName("周杰伦");
			//建立关联
			husband.setWife(wife);
			//没有抛出TransientObjectException
			//是由一对一关联映射的特性决定的,它必须先保存关联对象wife
			//这样它采用foreign映射策略才能取得关联对象的标识
			//也就是它默认了cascade属性
			session.save(husband);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testSave2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Wife wife = new Wife();
			wife.setName("昆凌");
			
			Husband husband = new Husband();
			husband.setName("周杰伦");
			//建立关联
			husband.setWife(wife);
			//只能将IdCard保存,不能将Person保存
			//因为关系的维护端在Person端,IdCard不知道Person的存在
			session.save(wife);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
	}

	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			Husband husband = (Husband)session.load(Husband.class, 1);
			System.out.println("husband.name=" + husband.getName());
			System.out.println("husband.wife=" + husband.getWife().getName());
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
}

 

双向主键关联:

参照单向主键关联,只需修改Wife映射文件:

 



	

	
		
			
		
		
		
	
测试类:
package com.java.hibernate;

import junit.framework.TestCase;

import org.hibernate.Session;

public class Test extends TestCase {
	
	public void testLoad() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Wife wife = (Wife) session.load(Wife.class, 1);
			System.out.println(wife.getName());
			System.out.println(wife.getHusband().getName());
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
		
	}
	
}

 

单向外键关联:

一对一唯一外键关联映射其实是多对一的特例,采用标签来映射,指定多的一端unique为true,这样就限制了多的一端的多重性唯一,就是这样来映射的。

Husband映射文件

 




	
		
			
		
		
		
	

 

Wife映射文件

 



	

	
		
			
		
		
	

 

测试类

 

package com.java.hibernate;

import junit.framework.TestCase;

import org.hibernate.Session;

public class Test extends TestCase{
	
	public void testSave1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Wife wife = new Wife();
			wife.setName("昆凌");
			
			Husband husband = new Husband();
			husband.setName("周杰伦");
			//建立关联
			husband.setWife(wife);
			//抛出TransientObjectException
			//因为IdCard为Transient状态
			session.save(husband);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testSave2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Wife wife = new Wife();
			wife.setName("昆凌");
			session.save(wife);
			
			Husband husband = new Husband();
			husband.setName("周杰伦");
			//建立关联
			husband.setWife(wife);
		
			session.save(husband);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
	}

	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			Husband husband = (Husband)session.load(Husband.class, 2);
			System.out.println("husband.name=" + husband.getName());
			System.out.println("husband.wife=" + husband.getWife().getName());
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
}

 

双向外键关联:

一对一唯一外键关联双向采用标签映射,必须指定标签中的property-ref属性为关系字段的名称

参照单向外键关联,只需修改Wife映射 文件

 



	

	
		
			
		
		
		
	
测试类:

 

 

package com.java.hibernate;

import junit.framework.TestCase;

import org.hibernate.Session;

public class Test extends TestCase {
	
	public void testLoad() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Wife wife = (Wife) session.load(Wife.class, 1);
			System.out.println(wife.getName());
			System.out.println(wife.getHusband().getName());
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
		
	}
	
}

 

点击复制链接 与好友分享!回本站首页
相关TAG标签 一对一
上一篇:WeakHashMap类源码解析
下一篇:LinkedHashMap类源码解析
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站