basedao.java

来自「Hibernate开发及整合应用大全 蔡雪焘编著 本书用典型的示例剖析Hiber」· Java 代码 · 共 82 行

JAVA
82
字号
/*
 * Created on Jun 10, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package dao;

import model.Student;
import model.StudentVO;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import persistence.HibernateUtil;

/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */

public class BaseDAO {
	public static void createObject(StudentVO stu_vo) throws HibernateException {
		Transaction tx = null;
		try {
			Session session = HibernateUtil.currentSession();	
			tx = session.beginTransaction();
            Student stu_po=new Student();
            BeanUtils.copyProperties(stu_po, stu_vo);
			session.save(stu_po);
		session.flush();

			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public static void mdfObject(StudentVO stu_vo) throws HibernateException {
		Transaction tx = null;
		try {
			Session session = HibernateUtil.currentSession();
			tx = session.beginTransaction();
            Student stu_po=new Student();
            BeanUtils.copyProperties(stu_po, stu_vo);
			session.update(stu_po);
			session.flush();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
	}

	public static void delObject(StudentVO stu_vo) throws HibernateException {
		Transaction tx = null;
		try {
			Session session = HibernateUtil.currentSession();
			tx = session.beginTransaction();
            Student stu_po=new Student();
            BeanUtils.copyProperties(stu_po, stu_vo);
			session.delete(stu_po);
			session.flush();
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		} finally {
			HibernateUtil.closeSession();
		}
	}
}

⌨️ 快捷键说明

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