Create session factory in Hibernate 4

前端 未结 8 2251
猫巷女王i
猫巷女王i 2020-11-30 19:13

I\'m having trouble generating a session factory in Hibernate 4. In Hibernate 3 I simple did:

org.hibernate.cfg.Configuration conf= HibernateUtil
    .getLim         


        
8条回答
  •  时光取名叫无心
    2020-11-30 20:04

    Yes, they have deprecated the previous buildSessionFactory API, and it's quite easy to do well.. you can do something like this..

    EDIT : ServiceRegistryBuilder is deprecated. you must use 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.");
        }
    

    For more reference and in depth detail, you can check the hibernate's official test case at https://github.com/hibernate/hibernate-orm/blob/master/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java function (buildSessionFactory()).

提交回复
热议问题