⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernatetest.java

📁 《基于Eclipse的开源框架技术与实战》[第7章]随书源码
💻 JAVA
字号:
package com.free.database.hibernate;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.Date;
import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
/**
 * <p>Title: Eclipse Plugin Development</p>
 * <p>Description: Free download</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: Free</p>
 * @author gan.shu.man
 * @version 1.0
 */
public class HibernateTest {
	public static SessionFactory sessionFactory;

	static {
		try {
			// 通过hibernate.properties创建Configuration对象
			Configuration config = new Configuration();
			config.addClass(Employee.class);
			// 构建SessionFactory
			sessionFactory = config.buildSessionFactory();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * 查找并打印出所有的员工
	 */
	public void findAllEmployee(OutputStream out) throws Exception {
		// 通过sessionFactory打开Session
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			List employee = session
					.find("from Employee as c order by c.name asc");
			for (Iterator it = employee.iterator(); it.hasNext();) {
				//打印员工信息
				printEmployee((PrintStream) out, (Employee) it.next());
			}
			tx.commit();

		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		} finally {
			//最终关闭session
			session.close();
		}
	}

	public void saveEmployee(Employee employee) throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			//保存员工信息
			session.save(employee);
			//提交事务
			tx.commit();

		} catch (Exception e) {
			if (tx != null) {
				//如果事务执行中出现异常,则回滚
				tx.rollback();
			}
			throw e;
		} finally {
			//关闭session
			session.close();
		}
	}

	public void loadAndUpdateEmployee(Long employee_id)
			throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			//装载Employee对象
			Employee employee = (Employee) session.load(Employee.class, employee_id);
			//修改Employee的地址
			employee.setEmail("testForHibernate@163.com");
			employee.setPassword("8888");
			employee.setPhone(80801111);
			
			tx.commit();

		} catch (Exception e) {
			if (tx != null) {
				//回滚
				tx.rollback();
			}
			throw e;
		} finally {
			//关闭session
			session.close();
		}
	}

	public void deleteAllEmployee() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			//删除所有的Employee对象
			session.delete("from Employee as c");
			tx.commit();

		} catch (Exception e) {
			if (tx != null) {
				//回滚
				tx.rollback();
			}
			throw e;
		} finally {
			//关闭session
			session.close();
		}
	}

	private void printEmployee(PrintStream out, Employee employee)
			throws Exception {

		out.println("------以下是" + employee.getName() + "的个人信息------");
		out.println("ID: " + employee.getId());
		out.println("口令: " + employee.getPassword());
		out.println("E-Mail: " + employee.getEmail());
		out.println("电话: " + employee.getPhone());
		out.println("地址: " + employee.getAddress());
		String sex = employee.getSex() == 'M' ? "男" : "女";
		out.println("性别: " + sex);
		String marriedStatus = employee.isMarried() ? "已婚" : "未婚";
		out.println("婚姻状况: " + marriedStatus);
		out.println("生日: " + employee.getBirthday());
		out.println("注册时间: " + employee.getRegisteredTime());
		out.println("自我介绍: " + employee.getDescription());

	}

	public void test(OutputStream out) throws Exception {
		Employee employee = new Employee();
		employee.setName("wang.ming");
		employee.setEmail("test@163.com");
		employee.setPassword("test");
		employee.setPhone(83731234);
		employee.setAddress("Shanghai");
		employee.setSex('M');
		employee.setDescription("I am a teacher");

		InputStream in = this.getClass().getResourceAsStream("favor.gif");
		byte[] buffer = new byte[in.available()];
		in.read(buffer);
		employee.setImage(buffer);
		employee.setBirthday(Date.valueOf("1983-01-01"));

		//保存Employee并打印
		saveEmployee(employee);
		findAllEmployee(out);
		//装载Employee、修改Employee并打印
		loadAndUpdateEmployee(employee.getId());
		findAllEmployee(out);

		//删除所有的Employee对象
		deleteAllEmployee();
	}

	public static void main(String args[]) throws Exception {
		new HibernateTest().test(System.out);
		//关闭sessionFactory
		sessionFactory.close();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -