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

📄 hibernatebase.java

📁 本代码是在eclipse下开发hibernate的典型实例
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -