HibernateUtil hibernate4.0以上

Posted 林**

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HibernateUtil hibernate4.0以上相关的知识,希望对你有一定的参考价值。

package com.test.bbs.util;

import org.hibernate.Session;
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 configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

 

以上是关于HibernateUtil hibernate4.0以上的主要内容,如果未能解决你的问题,请参考以下文章

HibernateUtil工具类的使用

如何在调用 servlet 之前静态初始化 HibernateUtil?

HibernateUtil.java

HibernateUtil

HibernateUtil

HibernateUtil工具类