Hibernate_映射_关联关系_一对多多对一映射1

48 篇文章 0 订阅
值类型的集合
	集合元素是普通类型
实体类型的集合
	集合元素是另外一个实体

	一对多								     多对一
Department.hbm.xml							Employee.hbm.xml
	要说明的信息有:						要说明的信息有:
	1,集合表(员工表)							1,关联的是什么类型
	2,集合外键								2,外键列
	3,集合元素 									
		关联的实例类型


部门映射文件

<?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 package="cn.itcast.f_hbm_oneToMany">
	<!-- 类名 -->
	<class name="Department" table="department">
		<id name="id" type="integer" column="id_">
			<generator class="native" />
		</id>
		<property name="name" type="string" column="name_" />
		<!-- employee属性,Set集合,表达的是本类与Employee的一对多关系 -->
		<set name="employee">
			<key column="departmentId" />
			<one-to-many class="Employee" />
		</set>
	</class>
</hibernate-mapping>
员工映射文件

<?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 package="cn.itcast.f_hbm_oneToMany">
	<!-- 类名 -->
	<class name="Employee" table="employee">
		<id name="id" type="integer" column="id_">
			<generator class="native" />
		</id>
		<property name="name" type="string" column="name_" />

		<!-- department属性,表达的是本类与Department的多对一的关系 -->
		<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
	</class>
</hibernate-mapping>
部门实例

package cn.itcast.f_hbm_oneToMany;

import java.util.HashSet;
import java.util.Set;

/**
 * 部门
 * 
 * @author 风清杨
 * @version V3.0
 * 
 */
public class Department {
	/** 部门id */
	private Integer id;
	/** 部门名称 */
	private String name;
	/** 员工信息 */
	private Set<Employee> employee = new HashSet<Employee>();// 关联的很多员工

	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 Set<Employee> getEmployee() {
		return employee;
	}

	public void setEmployee(Set<Employee> employee) {
		this.employee = employee;
	}

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + "]";
	}

}
员工实例

package cn.itcast.f_hbm_oneToMany;

/**
 * 员工部门
 * 
 * @author 风清杨
 * @version V3.0
 * 
 */
public class Employee {
	/** 员工编号 */
	private Integer id;
	/** 员工姓名 */
	private String name;

	/** 员工部门 */
	private Department department;// 关联的部门对象

	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 Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + "]";
	}

}

测试类

package cn.itcast.f_hbm_oneToMany;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * 应用程序操作类
 * 
 * @author 风清杨
 * @version V3.0
 * 
 */
public class App {
	private static SessionFactory sessionFactory = new Configuration()//
			.configure()//
			.addClass(Department.class)//
			.addClass(Employee.class)//
			.buildSessionFactory();

	// 保存,有关联关系
	@Test
	public void testSave() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			// ------------------------------------

			// 新建对象
			Department department = new Department();
			department.setName("开发部");

			Employee employee1 = new Employee();
			employee1.setName("张三");

			Employee employee2 = new Employee();
			employee2.setName("李四");

			// 关联起来
			employee1.setDepartment(department);
			employee2.setDepartment(department);
			department.getEmployee().add(employee1);
			department.getEmployee().add(employee2);

			// 保存
			session.save(department);
			session.save(employee1);
			session.save(employee2);
			// ------------------------------------
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	// 获取,可以获取到关联的对方
	@Test
	public void testGet() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			// ------------------------------------

			// 获取一方数据,并显示另一方信息
			//Department department = (Department) session.get(Department.class,
			// 1);
			// System.out.println(department);
			// System.out.println(department.getEmployee());
			Employee employee = (Employee) session.get(Employee.class, 1);
			System.out.println(employee);
			System.out.println(employee.getDepartment());

			// ------------------------------------
			tx.commit();
		} catch (RuntimeException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值