006——hibernate一对一关联映射

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

以夫妻为例(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映射文件:

<?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.java.hibernate.Husband" table="t_husband">
		<id name="id">
			<!-- 采用foreign生成策略,forgeign会取得关联对象的标识 -->
			<generator class="foreign">
				<!-- property只关联对象 -->
				<param name="property">wife</param>
			</generator>
		</id>
		<property name="name"></property>
		<!--
			one-to-one指示hibernate如何加载其关联对象,默认根据主键加载
			也就是拿到关系字段值,根据对端的主键来加载关联对象

			constrained="true表示,当前主键(husband的主键)还是一个外键
			参照了对端的主键(wife的主键),也就是会生成外键约束语句
		 -->
		<one-to-one name="wife" constrained="true"/>
	</class>
</hibernate-mapping>

Wife映射文件:

<?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.java.hibernate.Wife" table="t_wife">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
	</class>
</hibernate-mapping>

测试类

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映射文件:

<?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.java.hibernate.Wife" table="t_wife">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<one-to-one name="husband"></one-to-one>
	</class>
</hibernate-mapping>

测试类:

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

	}

}

单向外键关联:

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

Husband映射文件

<?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.java.hibernate.Husband" table="t_husband">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<many-to-one name="wife" unique="true"></many-to-one>
	</class>
</hibernate-mapping>

Wife映射文件

<?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.java.hibernate.Wife" table="t_wife">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
	</class>
</hibernate-mapping>

测试类

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

}

双向外键关联:

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

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

<?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.java.hibernate.Wife" table="t_wife">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<one-to-one name="husband" property-ref="wife"></one-to-one>
	</class>
</hibernate-mapping>

测试类:

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

	}

}
时间: 2024-10-19 16:09:44

006——hibernate一对一关联映射的相关文章

Hibernate一对一关联映射配置

一.一对一关联 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作: (1)保存员工档案的同时分配给员工一个账号. (2)加载员工档案的同时加载账号信息. 按照外键映射: 关系图: ①创建实体类:Resume.Users 并封装属性 public class Resume { //档案id private Integer resid; //档案名称 private Str

Hibernate 一对一关联映射

package com.entity; import javax.persistence.Entity; import javax.persistence.OneToOne; @Entity public class Husband extends BaseEntity { @OneToOne(mappedBy = "husband") private Wife wife; public Wife getWife() { return wife; } public void setWi

【SSH之旅】一步步学习Hibernate框架(二):一对一关联映射中的主键关联

一对一的映射在对象模型中是经常见到的,主要是将对象模型转换为关系模型就必须在映射文件中进行配置,重点是<one-to-one>标签的使用,有两种方式,第一是主键关联,第二是唯一外键关联,现在先来看第一种方式. 一对一的关联映射中的主键关联,就是说两个对象具有相同的主键值,以表明它们之间的一一对应的关系,数据库表不会有额外的字段来维护它们之间的关系,仅仅是通过表的主键来关联. 上图的Person类和IdCard类之间是一一对应关系,一个人就只能有一个身份证,一个身份证只能对应一个人,那么身份证的

【Hibernate】——一对一关联映射

两个对象之间如果是一对一的关系,如Person-IdCard.在Hibernate中有两种策略可以实现一对一的关联映射: 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库表不会有额外的字段来维护它们之间的关系,仅通过表得主键来关联. 唯一外键关联:外键关联,本来是用于多对一的配置,但是如果加上唯一的限制之后,也可以用来表示一对一关联关系: 而每种策略又分为两种方向的对应关系,即单向一对一关系和双向一对一关系.分类的原因一般是由需求决定的,单双向是站在不同的角度去看人

java之hibernate之基于外键的双向一对一关联映射

这篇讲解 基于外键的双向一对一关联映射 1.考察如下信息,人和身份证之间是一个一对一的关系.表的设计 2.类结构 Person.java public class Person implements Serializable{ private int id; private String name; private IdCard idCard; public Person() { } public Person(String name) { super(); this.name = name;

Hibernate注解----关联映射注解以及课程总结详解----图片版本

上一篇,记录了Hibernate注解----类级别注解以及属性注解详解 ,我们这一节主要讲解的是Hibernate注解----关联映射注解以及课程总结详解. 本节的主要内容: 第3章 关联映射注解 3-1 本章简介 3-2 实体之间的关系 3-3 一对一单向外键关联(一) 3-4 一对一单向外键关联(二) 3-5 一对一双向外键关联 3-6 一对一单向外键联合主键 3-7 多对一单向外键关联(一) 3-8 多对一单向外键关联(二) 3-9 一对多单向外键关联 3-9 一对多双向外键关联 3-10

mybatis 一对一关联映射实例

在实际项目开发中,经常存在一对一的关系,如一个人对应一张身份证信息,这就是一对一的关系.下面是一个简单的实例: 1.建表过程我就省略了,主要是一张Person表,一张IDCard表,其相关属性见步骤2Pojo类属性所示: 2.建立一个Person对象和一个IDCard对象: mybatis/pri/xiaoyang/otot/pojo/IDCard.java 1 public class IDCard implements Serializable { 2 private int id; //

Hibernate中一对一关联映射/组件映射

Hibernate映射:一对一关联 1.按照外键映射 2.按照主键映射 组件映射 下面以员工账号表和员工档案表(员工账号和档案表之间是一对一的关系)为例,介绍这两种映射关系,并使用这两种 映射方式分别完成以下持久化操作 (1)保存员工档案的同时分配给员工一个账号 (2)加载员工档案的同时加载账号信息 一:按照外键映射

hibernate 之 关联映射的基于外键的双向一对一关联

1. 人和身份证是一个一对一的关系,他们的表结构为: 2. 类结构:Person.java public class Person { private int id; private String name; private int age; private IdCard idCard; public Person() { } public Person(String name, int age) { super(); this.name = name; this.age = age; } pu