HibernateUtil工具类在hibernate3下与hibernate4下的区别
By:Roy.LiuLast updated:2014-01-02
以前一直在用Hibernate3 做应用系统,包括目前很多应用程序都是基于Hibernate3 写的,不过 Hibernate 4 都出来好久了,今天下载Hibernate 4.3 Final 版本,下来玩了一下, 发现 得到 sessionFactory 的方法过失了,虽然还是能用,但总感觉不爽. 所以看了下怎么写Hibernate4下的 Util类
Hibernate 3的 HibernateUtil
然而 return new Configuration().configure().buildSessionFactory(); 这句话已经过期了,于是在网上查找了一下,原来在 Hibernate4 中要这样做.
但是,同样发现出了问题,org.hibernate.service.ServiceRegistryBuilder 已经 deprecated 了,所以这还不是最终的方法,于是在hibernate 官方去搜索,发现了如下答案:
看到标粗体的部分了吗,这就是重点, 需要用 StandardServiceRegistryBuilder 代替 ServiceRegistryBuilder 这个类。
hibernate4 HibernateUtil
以上就是HIbernate4 的 Util 工具的一个基本演变过程.Use StandardServiceRegistryBuilder instead
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
From:一号门
Previous:delphi directui 做界面的一个例子
COMMENTS