hibernatebase.java

来自「本代码是在eclipse下开发hibernate的典型实例」· Java 代码 · 共 53 行

JAVA
53
字号
package com.jgao.hql.test.business.others;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public abstract class HibernateBase 
{
	protected SessionFactory sessionFactory;//会话工厂,用于创建会话
    protected Session session;//hibernate会话
    protected Transaction transaction; //hiberante事务
    
    public HibernateBase()throws HibernateException
    {
    	this.initHibernate();
    }
    
    // 帮助方法
    protected void initHibernate()
        throws HibernateException {

        // 装载配置,构造SessionFactory对象
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }
    
    /**
     *开始一个hibernate事务
     */
    protected void beginTransaction()
        throws HibernateException {

        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }
    
    /**
     *结束一个hibernate事务。
     */
    protected void endTransaction(boolean commit)
        throws HibernateException {

        if (commit) {
            transaction.commit();
        } else {
           //如果是只读的操作,不需要commit这个事务。
            transaction.rollback();
        }
         session.close();
    }
}

⌨️ 快捷键说明

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