欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate_映射_关联关系_cascade属性、单向关联

程序员文章站 2022-05-02 08:17:22
...
		<!-- employee属性,Set集合,表达的是本类与Employee的一对多关系 
			class属性:关联的实体类型
			key子元素:对方表中的外键列(多方的那个表)
			
			inverse属性:
				默认为false,表示本方维护关联关系。
				如果为true,表示本方不维护关联关系。
				只是影响是否能设置外键的值(设成有效值或者是null值),对获取信息没有影响
			
			cascade属性:
				默认为none,代表不级联。
				级联是指操作主对象时,对关联的对象也做相同的操作
				可设为:delete,save-update,all,none ...
		-->
		<set name="employee" cascade="all">
			<key column="departmentId" />
			<one-to-many class="Employee" />
		</set>

一对一,一对多,使用级联
	cascade属性:
		默认为none,代表不级联。
		级联是指操作主对象时,对关联的对象也做相同的操作
		可设为:delete,save-update,all,none ...
		
		<set name="employee" cascade="all">
			<key column="departmentId" />
			<one-to-many class="Employee" />
		</set>
双向关联
单向关联:
	单向多对一
	单向一对多
	单向多对多


值类型的集合
	集合元素是普通类型
实体类型的集合
	集合元素是另外一个实体

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



<?xml version="1.0"?>



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 = new HashSet();// 关联的很多员工

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

	public void setEmployee(Set 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();
		}
	}
}

维护关联关系:
	就是设置外键列的值。		
			// 解除关联关系
	@Test
	public void testRemoveRelation() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			// ------------------------------------

			// 从员工解除
			// Employee employee = (Employee) session.get(Employee.class, 1);
			// employee.setDepartment(null);

			// 从部门解除(与inverse有关系,为false时可以解除)
			Department department = (Department) session.get(Department.class,
					1);
			department.getEmployee().clear();

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

	// 删除对象,对关联对象的影响
	@Test
	public void testDelete() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			// ------------------------------------

			// 删除员工方(多方)对对方没有影响
			// Employee employee = (Employee) session.get(Employee.class, 2);
			// session.delete(employee);

			// 删除部门方(一方)
			// a,如果没有关联的员工:
			// b,如果有关联的员工且inverse=true,由于不能维护关联关系,所以会直接执行删除,就会有异常。
			// c,如果有关联的员工且inverse=false,由于可以维护关联关系,它就会先把关联的员工的外键列设为null值,再删除自已。
			Department department = (Department) session.get(Department.class,
					1);
			// department.getEmployee().clear();
			session.delete(department);

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