Hibernate_部门查询

48 篇文章 0 订阅
package cn.itcast.test.dao;

import java.util.List;

import cn.itcast.test.entity.Department;

/**
 * 接口:部门
 * 
 * @author 风清杨
 * @version V5.0
 */
public interface DepartmentDao {
	/**
	 * 增加一个部门
	 * 
	 * @param department
	 *            要增加的部门
	 */
	public abstract void save(Department department);

	/**
	 * 更新一个部门的信息(更改名称)
	 * 
	 * @param department
	 *            要更新的部门
	 */
	public abstract void update(Department department);

	/**
	 * 把部门中所有的员工都取消关联
	 * 
	 * @param id
	 *            要与员工取消关联的部门编号
	 */
	public abstract void removeRlieve(Long id);

	/**
	 * 删除一个部门,并把所有的子部门删掉,如果部门中有员工,则不能删除。
	 * 
	 * @param id
	 *            要删除的部门id
	 */
	public abstract void delete(Long id);

	/**
	 * 根据id查询部门
	 * 
	 * @param id
	 *            部门id
	 * @return 返回部门
	 */
	public abstract Department getById(Long id);

	/**
	 * 查询最顶级部门,按id升序排列(最顶级的就是没有上级的部门)
	 * 
	 * @return 其顶级部门
	 */
	public abstract List<Department> parentAsc();

	/**
	 * 查询某部门的所有子部门,按id升序排序
	 * 
	 * @param department
	 *            要查询的部门
	 * @return 返回其子部门
	 */
	public abstract List<Department> childrenAsc(String department);

	/**
	 * 员工的总数量,部门的总数量,每个部门中员工的数量。
	 * 
	 * @param department
	 *            要查询的部门
	 * @return 返回总数
	 */
	public abstract QueryResult findCount();
}

package cn.itcast.test.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.itcast.test.dao.DepartmentDao;
import cn.itcast.test.dao.QueryResult;
import cn.itcast.test.entity.Department;

/**
 * 实现:部门
 * 
 * @author 风清杨
 * @version V5.0
 */
public class DepartmentDaoImpl implements DepartmentDao {

	@Override
	public void save(Department department) {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// --------------------------------

			session.save(department);// 保存

			// --------------------------------
			// 提交事务
			tx.commit();
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}
	}

	@Override
	public void update(Department department) {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// --------------------------------

			session.update(department);

			// --------------------------------
			// 提交事务
			tx.commit();
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}

	}

	@Override
	public void removeRlieve(Long id) {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// --------------------------------

			Department department = (Department) session.get(Department.class, id);
			department.getEmployee().clear();

			session.update(department);
			// --------------------------------
			// 提交事务
			tx.commit();
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}
	}

	@Override
	public void delete(Long id) {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// --------------------------------
			// 删除一个部门,并把所有的子部门删掉,如果部门中有员工,则不能删除。
			Department department = (Department) session.get(Department.class, 1L);
			if (!department.getEmployee().isEmpty()) {
				System.out.println("该部门有员工");
			} else {
				session.delete(department);
				System.out.println("删除成功");
			}

			// --------------------------------
			// 提交事务
			tx.commit();
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> parentAsc() {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// --------------------------------

			List<Department> list = session.createQuery(//
					"from Department e where e.parent is null order by e.id asc")//
					.list();

			// --------------------------------
			// 提交事务
			tx.commit();
			return list;
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> childrenAsc(String department) {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// --------------------------------

			List<Department> list = session.createQuery(//
					"select d.children from Department d where d.name=:name")//
					.setParameter("name", department)//
					.list();

			// --------------------------------
			// 提交事务
			tx.commit();
			return list;
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}
	}

	@Override
	public QueryResult findCount() {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// --------------------------------
			// 员工的总数量
			Long employeeCount = (Long) session.createQuery(//
					"select count(e) from Employee e")//
					.uniqueResult();

			// 部门的总数量
			Long departmentCount = (Long) session.createQuery(//
					"select count(d) from Department d")//
					.uniqueResult();
			// 每个部门中员工的数量。
			List<Object> list = session.createQuery(//
					"select d.name,count(e.name) from Department d join d.employee e group by d.name")//
					.list();

			// --------------------------------
			// 提交事务
			tx.commit();
			return new QueryResult(employeeCount, departmentCount, list);
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}
	}

	@Override
	public Department getById(Long id) {
		// 打开Session
		Session session = Utils.getopensession();
		// 创建事务
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// ----------------------------------

			Department department = (Department) session.get(Department.class, id);

			// ----------------------------------
			// 提交事务
			tx.commit();
			return department;
		} catch (RuntimeException e) {
			// 回滚事务
			tx.rollback();
			throw e;
		} finally {
			// 释放资源
			session.close();
		}
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值