(五)HibernateSessionFactory分析

        在MyEclipse里面为了简化开发提供有这样的工具类,这个工具类,这个工具类的主要目的是取得SessionFactroy以及Session对象.现在最为重要的实际上是Session对象,所有的数据操作由此展开.

范例:分析HibernateSessionFactory

package cn.zwb.dbc;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateSessionFactory {
		//表示提供有ThreadLocal对象保存,适合于进行线程的准切处理
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static org.hibernate.SessionFactory sessionFactory;//连接工厂
	
    private static Configuration configuration = new Configuration();//读取配置文件
    private static ServiceRegistry serviceRegistry; //服务注册类
    	//静态代码块,可以在类加载的时候执行一次
	static {
    	try {
			configuration.configure();//读取配置文件
			serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
				//在静态块中就已经准备好了SessionFactory类的对象
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		} catch (Exception e) {
			System.err.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }
    private HibernateSessionFactory() {  //构造方法私有化,本类不需要提供构造
    }
	
	/**
     *取得Session对象,对象是通过ThreadLocal类取得的,如果没有保存的Session,那么会重新连接
     *
     *  @return Session   
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        	//为了防止用户可能重复的使用Session对象,是通过保存在ThreadLocal类中的Session直接使用的
    	Session session = (Session) threadLocal.get();

		if (session == null || !session.isOpen()) {//如果第一次使用或者关闭了
			if (sessionFactory == null) {		//判断此时存在有SessionFactory类对象
				rebuildSessionFactory();		//如果SessionFactory不存在,则重新创建一个SessionFactroy
			}
			
			//判断此时是否取得了SessionFactory类对象,如果取得,则使用openSession()打开新Session,否则返回空
			session = (sessionFactory != null) ? sessionFactory.openSession()
					: null;
			threadLocal.set(session);  //为了防止可能重复使用Session
		}

        return session;
    }

	/**
     *  重新进行SessionFactory类对象的创建
     *
     */
	public static void rebuildSessionFactory() {
		try {
			configuration.configure();
			serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		} catch (Exception e) {
			System.err.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}

	/**
     *  所有操作的最后一定要关闭Session,在业务层中关闭
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();//取得已有的Session
        threadLocal.set(null);//清空ThreadLocal的保存

        if (session != null) {//将Session进行关闭
            session.close();
        }
    }

	/**
	 *取得SessionFactory类的对象
     *  return session factory
     *
     */
	public static org.hibernate.SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	/**
	 * 	取得Configuration的对象
     *  return hibernate configuration
     *
     */
	public static Configuration getConfiguration() {
		return configuration;
	}

}

        这个类只要使用就可以取得Session.SessionFactroy,Configuration类的对象,至于如何取得,暂时不用关心,因为Hibernate不可能单独使用,都会结合到Spring上使用,

范例:使用这个连接工厂类

package cn.zwb.test;
import cn.zwb.dbc.HibernateSessionFactory;
import cn.zwb.pojo.Member;

public class TestemberInsertDemo {
	public static void main(String[] args) {
		//设置vo属性内容内容的操作应该由控制层完成
		Member vo=new Member();
		vo.setMid("ww低洼地zwb请求aaa");
		vo.setName("SHITH");
		vo.setAge(10);
		vo.setSalary(1000.0);
		vo.setNode("wwwwwwww");
		vo.setBirthday(151561.0);
		//2.数据的保存操作应该由数据层完成
		HibernateSessionFactory.getSession().save(vo);
		//3.事务的Session的关闭应该由业务层完成
		HibernateSessionFactory.getSession().beginTransaction().commit();
		HibernateSessionFactory.closeSession();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值