如何从Hibernate 4.3.4.Final中检索sessionFactory?

时间:2014-03-13 02:11:46

标签: java hibernate sessionfactory

以前我使用以下代码来配置sessionFactory,但是在将我的hibernate版本从4.2.1.Final升级到4.3.4.Final之后,我无法使用以下代码检索sessionFactory,因为ServiceRegistryBuilder()是弃用。

我使用this link来创建它,但提供的函数没有返回任何东西,因此它会遇到预编译错误。

 private static SessionFactory configureSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;
        } catch (HibernateException e) {
            System.out.append("** Exception in SessionFactory **");
            e.printStackTrace();
        }
        return sessionFactory;
    }

     static {
    try {
        sessionFactory = configureSessionFactory();
    } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
    }
}

private HibernateUtil() {
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static Session getSession() throws HibernateException {
    Session session = threadLocal.get();

    if (session == null || !session.isOpen()) {
        if (sessionFactory == null) {
            rebuildSessionFactory();
        }
        session = (sessionFactory != null) ? sessionFactory.openSession() : null;
        threadLocal.set(session);
    }

    return session;
}

public static void rebuildSessionFactory() {
    try {
        sessionFactory = configureSessionFactory();
    } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
    }
}

public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);

    if (session != null) {
        if (session.isOpen()) {
            session.close();
        }
    }
}

3 个答案:

答案 0 :(得分:7)

在4.3中,您应该使用StandardServiceRegistryBuilder代替。

import org.hibernate.boot.registry.StandardServiceRegistryBuilder

StandardServiceRegistryBuilder已添加为ServiceRegistryBuilder的新。其余的代码应该是相同的。我发现这个记录的唯一地方#34;清楚"在ServiceRegistryBuilder的4.3 JavaDocs中。

我没注意到的一件事是他们将ServiceRegistryBuilder.buildServiceRegistry()方法更改为StandardServiceRegistryBuilder.build()。因此,它将要求您更改构建过程的这一部分:

new ServiceRegistryBuilder()
        .applySettings(configuration.getProperties())
        .buildServiceRegistry();

变为:

new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties())
        .build();

答案 1 :(得分:0)

我测试了这个,它适用于Hibernate 4.3.6

public class HUtil{   
  private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory createSessionFactory() {
  Configuration configuration = new Configuration();
  configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().
 applySettings(configuration.getProperties()).build();
  sessionFactory = configuration.configure().      buildSessionFactory(serviceRegistry);
  return sessionFactory;
}

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

答案 2 :(得分:-2)

是的,他们已弃用以前的ServiceRegistryBuilder(),以下是如何使用Hibernate 4.3.4

public void testConnection() throws Exception {

            logger.info("Trying to Connect With a DataBase.");
            Configuration configuration = new Configuration();
            configuration.configure("Your.cfg.xml");
            ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration
                            .getProperties());
            SessionFactory sessionFactory = configuration
                            .buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
            Session session = sessionFactory.openSession();
            logger.info("Database connection created successfuly.");
    }

来源:Create session factory in Hibernate 4