我在Hibernate 4中生成会话工厂时遇到问题。在Hibernate 3中,我很简单地做到了:
org.hibernate.cfg.Configuration conf= HibernateUtil .getLimsInitializedConfiguration(systemConfiguration .getHibernateconfFile()); SessionFactory sf = conf.configure().buildSessionFactory();
现在,我需要将ServiceRegistry类传递给buildSessionFactory,但是Javadocs对如何执行此操作非常含糊。有小费吗?
是的,他们已经弃用了先前的buildSessionFactory API,并且做起来很容易..您可以执行类似的操作。
编辑 :不推荐使用ServiceRegistryBuilder。您必须使用StandardServiceRegistryBuilder
public void testConnection() throws Exception { logger.info("Trying to create a test connection with the database."); Configuration configuration = new Configuration(); configuration.configure("hibernate_sp.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); logger.info("Test connection with the database created successfuly."); }
有关更多参考和更详细的信息,您可以在以下网址查看hibernate的官方测试用例:https://github.com/hibernate/hibernate- orm/blob/master/hibernate-testing/src/main/java/org/hibernate/testing /junit4/BaseCoreFunctionalTestCase.java函数(buildSessionFactory())。