package org.hibernate_one;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.util.ConfigHelper;
import org.hibernate_one.entity.Dept;
import org.junit.Test;


/*
 * 单元测试
 * 记得加@Test导入包
 */


public class TestUtil {
	@Test
	public void testADept(){
		/*
		 *1.读取配置文件
		 *2.创建session工厂
		 *3.打开session
		 *4.开始事务
		 *5.持久化操作(增删改)
		 *6.提交事务或者回滚事务
		 *7.关闭session
		  */
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFac = conf.buildSessionFactory();
		Session session = sessionFac.openSession();
		Transaction tx = session.beginTransaction();
		Dept dept = new Dept(70, "bdqn", "jinan");
		try {
			//session.save(dept);
			//tx.commit();
			
			//查询
			//Dept dep=(Dept)session.get(Dept.class,50 );
			//System.out.println(dep.getdName());
			//修改
			//dep.setdName("oldNme");
			//删除
			//session.delete(dep);
			
			//不是持久化状态提交
			//session.update(dept);
			//有的化修改,没有的话添加
			session.saveOrUpdate(dept);
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
			tx.rollback();// 回滚事务
		} finally {
			session.close();
		}
	}


}