HibernateUtil工具类在hibernate3下与hibernate4下的区别

以前一直在用Hibernate3 做应用系统,包括目前很多应用程序都是基于Hibernate3 写的,不过 Hibernate 4 都出来好久了,今天下载Hibernate 4.3 Final 版本,下来玩了一下, 发现 得到 sessionFactory 的方法过失了,虽然还是能用,但总感觉不爽. 所以看了下怎么写Hibernate4下的 Util类

  Hibernate 3的 HibernateUtil
程序代码 程序代码

package com.yihaomen.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void shutdown() {
        getSessionFactory().close();
    }

}


然而 return new Configuration().configure().buildSessionFactory(); 这句话已经过期了,于是在网上查找了一下,原来在 Hibernate4 中要这样做.

程序代码 程序代码

package com.yihaomen.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil
{
    private static final SessionFactory sessionFactory;
    static
    {
        try
        {
            Configuration cfg = new Configuration().configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).build();

            sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        }
        catch (Throwable e)
        {
            throw new ExceptionInInitializerError(e);
        }
    }

    private HibernateUtil()
    {
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

}



但是,同样发现出了问题,org.hibernate.service.ServiceRegistryBuilder 已经 deprecated 了,所以这还不是最终的方法,于是在hibernate 官方去搜索,发现了如下答案:

程序代码 程序代码

Class ServiceRegistryBuilder

java.lang.Object
org.hibernate.boot.registry.StandardServiceRegistryBuilder
org.hibernate.service.ServiceRegistryBuilder
Deprecated.
Use StandardServiceRegistryBuilder instead

@Deprecated
public class ServiceRegistryBuilder
extends StandardServiceRegistryBuilder


看到标粗体的部分了吗,这就是重点, 需要用 StandardServiceRegistryBuilder  代替 ServiceRegistryBuilder 这个类。

hibernate4 HibernateUtil

程序代码 程序代码

package com.yihaomen.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil
{
    private static final SessionFactory sessionFactory;
    static
    {
        try
        {
            Configuration cfg = new Configuration().configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();

            sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        }
        catch (Throwable e)
        {
            throw new ExceptionInInitializerError(e);
        }
    }

    private HibernateUtil()
    {
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

}

以上就是HIbernate4 的 Util 工具的一个基本演变过程.Use StandardServiceRegistryBuilder instead

除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
[本日志由 轻舞肥羊 于 2014-01-02 10:05 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: hibernate
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.