码迷,mamicode.com
首页 > 系统相关 > 详细

Hibernate关联关系映射之一对一关联关系

时间:2014-09-27 12:04:59      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:hibernate   one-to-one   

人和身份证之间就是一个典型的一对一关联关系。实现一对一关联关系映射的方式有两种一种是基于外键,一种是基于主键,下面我们先看基于外键的关联方式

首先看他们的实体类

Person类

package entity;

public class Person {
	private Integer id;
	private String name;
	private IdCard idCard;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public IdCard getIdCard() {
		return idCard;
	}
	public void setIdCard(IdCard idCard) {
		this.idCard = idCard;
	}
}

然后看IdCard类

package entity;

public class IdCard {
	private Integer id;
	private String number;
	private Person person;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	
	
}

这里在Person类和IdCard类中分别有一个对方类型的属性。

然后看他们之间的关联关系映射,首先看Person.hbm.xml

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

<hibernate-mapping>
    <class name="entity.Person" table="Person" schema="MYHR">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="NAME" not-null="true" />
        </property>
        <!-- idCard属性,IdCard类型,表达本类与IdCard的一对一关联 -->
        <!-- 采用基于外键的一对一,本方无外键方 -->
        <!-- property-ref属性:对方映射中外键类对应的属性名 -->
        <one-to-one name="idCard" class="entity.IdCard" property-ref="person"> 
        </one-to-one>
    </class>
</hibernate-mapping>

然后看一下IdCard.hbm.xml文档

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="entity.IdCard" table="IdCard" schema="MYHR">
        <id name="id">
            <column name="ID"/>
            <generator class="assigned" />
        </id>
        <property name="number">
            <column name="NAME" not-null="true" />
        </property>
        <!-- person属性,Person类型,表示与Person类的一对一关联关系 -->
        <!-- 采用基于外键的一对一关联方式,本方有外键 -->
        <many-to-one name="person" column="personId" class="entity.Person" unique="true"></many-to-one>
    </class>
</hibernate-mapping>

这样Person和IdCard之间的一对一关联关系就搭建好了,然后看一下测试类

package test;

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.Transaction;

import entity.IdCard;
import entity.Person;
import factory.HibernateSessionFactory;

public class Test {

	private Session session = null;
	private Transaction tran = null;
	@org.junit.Test
	public void save() {
		session = HibernateSessionFactory.getSession();
		tran = session.beginTransaction();
		try{
			//新建对象
			Person p = new Person();
			p.setId(1);
			p.setName("张三");
			
			IdCard idCard = new IdCard();
			idCard.setId(1);
			idCard.setNumber("10001");
			
			p.setIdCard(idCard);
			idCard.setPerson(p);
			
			session.save(p);
			session.save(idCard);
			
			tran.commit();
		}catch(Exception e){
			tran.rollback();
		}
	}

	@org.junit.Test
	public void Get() {
		session = HibernateSessionFactory.getSession();
		tran = session.beginTransaction();
		try{
			Person person = (Person) session.get(Person.class, 1);
			IdCard idCard = person.getIdCard();
			System.out.println(person.getName()+"身份证号:"+idCard.getNumber());
			tran.commit();
		}catch(Exception e){
			tran.rollback();
		}
	}
	//解除关系
	@org.junit.Test
	public void RemoveRelation() {
		session = HibernateSessionFactory.getSession();
		tran = session.beginTransaction();
		try{
			//从有外键方接触关联关系可以,不可以从无外键方解除
			IdCard idCard = (IdCard)session.get(IdCard.class, 1);
			idCard.setPerson(null);
			tran.commit();
		}catch(Exception e){
			tran.rollback();
		}
	}
	//删除
	@org.junit.Test
	public void DeleteRelation() {
		session = HibernateSessionFactory.getSession();
		tran = session.beginTransaction();
		try{
			//如果没有关联对方,可以直接删除。 
			//如果有关联对方,且可以维护关联关系,他就会先删除关联关系,在删除自己。
			//如果有关联关系,且不能维护关联关系,所以会执行删除自己,并且抛出异常。
			IdCard idCard = (IdCard)session.get(IdCard.class, 1);
			session.delete(idCard);
			tran.commit();
		}catch(Exception e){
			tran.rollback();
		}
	}
}

这就是基于外键方式,基于主键的方式只需要在以上基础修改一下映射文件即可,下面分别看一下修改后的映射文件

首先看Person.hbm.xml

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

<hibernate-mapping package="entity">
    <class name="entity.Person" table="Person" schema="MYHR">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="NAME" not-null="true" />
        </property>
        <!-- idCard属性,IdCard类型,表达本类与IdCard的一对一关联 -->
        <!-- 采用基于主键的一对一,本方无外键方 -->
        <one-to-one name="idCard" class="entity.IdCard"/>
      
    </class>
</hibernate-mapping>

然后看IdCard.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
	<class name="entity.IdCard" table="IdCard" schema="MYHR">
		<id name="id" type="int">
			<column name="ID" />
			<!-- 当使用基于主键的一对一映射时,有外键方的主键生成策略是foreign, -->
			<!-- 参数property:生成主键值时所根据的对象 -->
			<generator class="foreign">
			    <param name="property">person</param>
			</generator>
		</id>
		<property name="number" type="string">
			<column name="NAME" not-null="true" />
		</property>
		<!-- person属性,Person类型,表示与Person类的一对一关联关系 -->
		<!-- 采用基于主键的一对一关联方式,本方有外键 -->
		<one-to-one name="person" class="entity.Person"
			constrained="true" />
	</class>
</hibernate-mapping>

 

Hibernate关联关系映射之一对一关联关系

标签:hibernate   one-to-one   

原文地址:http://blog.csdn.net/u011740475/article/details/39609249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!