Hibernate (10) one-to-one association mapping

This blog series is summarized here: Hibernate summary

One to one association mapping

The source code project file is: hibernate4.3_ & hibernate4.3_

One to one association mapping

Class diagram:

Table relationship:

1. Create model class

You must give the IdCard a primary key, and specify a one-to-one relationship.

package com.wyx.hiber.model;

public class IDCard
{

	/**
	 * Provide primary key of ID card
	 */
	private Integer cardId;

	private String cardNo;

	/**
	 * There is an employee's attribute in the ID card class, reflecting one-to-one
	 */
	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">
		<! -- specify the primary key name. It is recommended to use the primary key name of emp side -- >
		<id name="cardId" column="emp_no">
			<! -- use foreign key to generate primary key -- >
			<generator class="foreign">
				<! -- specify the emp attribute at the end of idCard -- >
				<param name="property">emp</param>
			</generator>
		</id>
		<property name="cardNo" column="card_no"></property>
		<!-- 
			Specify a one-to-one relationship
			Name: the property name emp of the other end of the current end
			constrained: with foreign key constraints when creating tables
		 -->
		<one-to-one name="emp" constrained="true"></one-to-one>
	</class>	
</hibernate-mapping>

After the Hibernate mapping file is created, it should be registered in our hibernate.cfg.xml and then exported to the database.

3. One to one single end test

Preservation

public void testAdd()
{
	Session session = HibernateUtils.getSession();
	Transaction tx = session.beginTransaction();
	try
	{
		// Create staff
		Emp emp = new Emp();
		emp.setEname("Wei Yu Huang");
		emp.setAddress("Kunming");
		emp.setGender(1);
		emp.setBirthday(new Date());
		// Create ID card
		IDCard ic = new IDCard();
		ic.setCardNo("1234567890");
		// Specify a one-to-one relationship
		ic.setEmp(emp);
		// emp is saved automatically when ic is saved, because the primary key of ic is emp,
		// If emp does not save the primary key, it will not return, so emp must be saved before ic saving
		session.save(ic);
		tx.commit();
	} catch (Exception e)
	{
		e.printStackTrace();
		tx.rollback();
	} finally
	{
		HibernateUtils.closeResource(session);
	}
}



query

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

4. One to one bidirectional association mapping

Modify the model of emp and add the attribute of idcard.

Configuration in 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>
		<!-- 
			//One to one for two-way association mapping, viewed from the direction of arrow of database model
			name: Specifies the model attribute
		 -->
		<one-to-one name="card" cascade="save-update"></one-to-one>
	</class>
</hibernate-mapping>

Preservation

public void testAdd()
{
	Session session = HibernateUtils.getSession();
	Transaction tx = session.beginTransaction();
	try
	{
		// Create staff
		Emp emp = new Emp();
		emp.setEname("Wei Yu Huang");
		emp.setAddress("Kunming");
		emp.setGender(1);
		emp.setBirthday(new Date());
		// Create ID card
		IDCard ic = new IDCard();
		ic.setCardNo("1234567890");
		// Set the relationship between the employee and ID card. Because it is a two-way mapping, you need to specify a two-way relationship
		emp.setCard(ic);
		ic.setEmp(emp);
		// Save emp first.
		session.save(emp);
		// Save ic
		session.save(ic);
		tx.commit();
	} catch (Exception e)
	{
		e.printStackTrace();
		tx.rollback();
	} finally
	{
		HibernateUtils.closeResource(session);
	}
}

Using the cascade mode, you don't need to save the ic separately

query

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

Queries from the direction pointed by the arrow use join queries

If there is any mistake, please correct it!

449 original articles published, 210 praised, 80000 visitors+
His message board follow

Tags: Session Hibernate Attribute xml

Posted on Tue, 28 Jan 2020 23:00:18 -0800 by zelot1980