Hibernate (10) _ one association map

This series of blog summarized here: Hibernate Summary


Source project file: hibernate4.3_05 & hibernate4.3_06

One to one association mapping

FIG class:
Here Insert Picture Description
relational tables:
Here Insert Picture Description

1. Create a model class

IdCard have to give this a primary key, and specify one to one relationship.

package com.wyx.hiber.model;

public class IDCard
{

	/**
	 * 提供身份证的主键
	 */
	private Integer cardId;

	private String cardNo;

	/**
	 * 在身份证的类中有一个员工的属性,体现一对一
	 */
	private Emp emp;

	public Integer getCardId()
	{
		return cardId;
	}

	public void setCardId(Integer cardId)
	{
		this.cardId = cardId;
	}

	public String getCardNo()
	{
		return cardNo;
	}

	public void setCardNo(String cardNo)
	{
		this.cardNo = cardNo;
	}

	public Emp getEmp()
	{
		return emp;
	}

	public void setEmp(Emp emp)
	{
		this.emp = emp;
	}

	@Override
	public String toString()
	{
		return "IDCard [cardId=" + cardId + ", cardNo=" + cardNo + "]";
	}

}

2, configuration mapping file

<?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="com.wyx.hiber.model">
	<class name="IDCard" table="t_id_card">
		<!-- 指定主键名称,建议使用emp端的主键名称 -->
		<id name="cardId" column="emp_no">
			<!-- 使用外键的方式来生成主键 -->
			<generator class="foreign">
				<!-- 指定idCard这一端的emp属性 -->
				<param name="property">emp</param>
			</generator>
		</id>
		<property name="cardNo" column="card_no"></property>
		<!-- 
			指定一对一的关系
			name:当前端的的另一个一的一端的属性名称emp
			constrained:建表时带有外键约束
		 -->
		<one-to-one name="emp" constrained="true"></one-to-one>
	</class>	
</hibernate-mapping>

After you create the Hibernate mapping file you want to register to our hibernate.cfg.xml, and then export the database can be.
Here Insert Picture Description
Here Insert Picture Description

3, one single-ended test

Storage

public void testAdd()
{
	Session session = HibernateUtils.getSession();
	Transaction tx = session.beginTransaction();
	try
	{
		// 创建员工
		Emp emp = new Emp();
		emp.setEname("魏宇轩");
		emp.setAddress("昆明");
		emp.setGender(1);
		emp.setBirthday(new Date());
		// 创建身份证
		IDCard ic = new IDCard();
		ic.setCardNo("1234567890");
		// 指定一对一的关系
		ic.setEmp(emp);
		// 保存ic的时候自动的保存了emp,因为ic的主键是emp的主键,
		// 如果emp不保存主键就不会返回,所以在ic保存之前必须先保存emp
		session.save(ic);
		tx.commit();
	} catch (Exception e)
	{
		e.printStackTrace();
		tx.rollback();
	} finally
	{
		HibernateUtils.closeResource(session);
	}
}

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Inquire

public void testQuery()
{
	Session session = HibernateUtils.getSession();
	try
	{
		IDCard card = (IDCard) session.get(IDCard.class, 1);
		System.out.println(card);
		System.out.println(card.getEmp());
	} catch (Exception e)
	{
		e.printStackTrace();
	} finally
	{
		HibernateUtils.closeResource(session);
	}
}

Here Insert Picture Description

4, one to one mapping bidirectional associations

Emp modify the model, adding attributes idcard this end.
Here Insert Picture Description
In the configuration of emp.hbm.xml

<?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="com.wyx.hiber.model">
	<class name="Emp" table="t_emp">
		<id name="empNo" column="emp_no">
			<generator class="native"></generator>
		</id>
		<property name="ename"></property>
		<property name="birthday"></property>
		<property name="gender"></property>
		<property name="address"></property>
		<!-- 
			双向关联映射的一对一,从数据库模型的箭头指向方向来看
			name:指定箭头背向端的model属性
		 -->
		<one-to-one name="card" cascade="save-update"></one-to-one>
	</class>
</hibernate-mapping>

Storage

public void testAdd()
{
	Session session = HibernateUtils.getSession();
	Transaction tx = session.beginTransaction();
	try
	{
		// 创建员工
		Emp emp = new Emp();
		emp.setEname("魏宇轩");
		emp.setAddress("昆明");
		emp.setGender(1);
		emp.setBirthday(new Date());
		// 创建身份证
		IDCard ic = new IDCard();
		ic.setCardNo("1234567890");
		// 设置员工和身份证的关系,由于是双向映射需要指定双向的关系
		emp.setCard(ic);
		ic.setEmp(emp);
		// 先保存emp
		session.save(emp);
		// 保存ic
		session.save(ic);
		tx.commit();
	} catch (Exception e)
	{
		e.printStackTrace();
		tx.rollback();
	} finally
	{
		HibernateUtils.closeResource(session);
	}
}

Use cascade would not have saved a single ic
Here Insert Picture Description
Here Insert Picture Description

Inquire

public void testQuery()
{
	Session session = HibernateUtils.getSession();
	try
	{
		Emp emp = (Emp) session.get(Emp.class, 1);
		System.out.println(emp);
		System.out.println(emp.getCard());
	} catch (Exception e)
	{
		e.printStackTrace();
	} finally
	{
		HibernateUtils.closeResource(session);
	}
}

From the direction of the arrow pointing to the query using the join query
Here Insert Picture Description

If wrong, please correct me!

Published 449 original articles · won praise 210 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_36260974/article/details/104106477