码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate之实现一对多、多对一映射关联关系

时间:2015-07-16 09:43:01      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

一对多、多对一这种关系在现实生活中很多,例如部门与员工的关系,学校里班级与学生的关系...

那么在具体的系统实现中如果i实现这种关联关系呢?这里以部门和员工的关系为例。

部门实体类

package test.hibernate.hbmOneToMany;

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

public class Department {

	private Integer id;
	private String name;
	private Set<Employee> employees = 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> getEmployees() {
		return employees;
	}

	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[employee:id=" + id + ",name=" + name + "]";
	}

}
员工实体类

package test.hibernate.hbmOneToMany;

public class Employee {

	private Integer id;
	private String name;
	private Department department=new 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;
	}

}
部门映射Department.hbm.xml

<?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="test.hibernate.hbmOneToMany">
	<class name="Department" table="department">
		<id name="id" type="integer" column="id">
			<generator class="native" />
		</id>
		<property name="name"/>
		<set name="employees">
			<key column="departmentId"></key>
			<one-to-many class="Employee" />
		</set>
	</class>
</hibernate-mapping>
员工映射文件Employee.hbm.xml

<?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="test.hibernate.hbmOneToMany">
	<class name="Employee" table="employee">
		<id name="id" type="integer" column="id">
			<generator class="native" />
		</id>
		<property name="name"/>
		<many-to-one name="department" class="Department" column="departmentId"></many-to-one>

	</class>
</hibernate-mapping>
测试类App

package test.hibernate.hbmOneToMany;

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

public class App {

	private static SessionFactory sessionFactory = new Configuration()//
			.configure()//
			.addClass(Department.class)// 添加Hibernate实体类(加载对应的映射文件)
			.addClass(Employee.class)//
			.buildSessionFactory();

	@Test
	public void testSave() throws Exception {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		// --------------------------------------------

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

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

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

		// 关联起来
		employee.setDepartment(department);
		employee2.setDepartment(department);
		department.getEmployees().add(employee);
		department.getEmployees().add(employee2);

		// 保存
		session.save(department);
		session.save(employee);
		session.save(employee2);

		// --------------------------------------------
		session.getTransaction().commit();
		session.close();
	}

	// 获取到部门关联的员工
	@Test
	public void testGet() throws Exception {
		Session session = sessionFactory.openSession();
		session.beginTransaction();

		// 获取数据
		Department department = (Department) session.get(Department.class, 1);
		System.out.println(department.getId());
		System.out.println(department.getName());
		System.out.println(department.getEmployees());

		session.getTransaction().commit();
		session.close();
	}

	// 删除部门及对员工的影响
	public void testDelete() throws Exception {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		// --------------------------------------------

		// 构建对象

		// --------------------------------------------
		session.getTransaction().commit();
		session.close();
	}
}
技术分享


技术分享


技术分享


技术分享


原文出处:http://blog.csdn.net/lindonglian/article/details/46906487

版权声明:本文为博主原创文章,未经博主允许不得转载。如需转载,请注明出处:http://blog.csdn.net/lindonglian

Hibernate之实现一对多、多对一映射关联关系

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/46906487

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