Hibernate - ServiceRegistryBuilder

前端 未结 9 1904
眼角桃花
眼角桃花 2020-12-28 08:30

I\'m just trying to learn Hibernate (version 4 final) but I have a problem when trying to create the session factory. Here is some code related to the problem:

hi

9条回答
  •  孤城傲影
    2020-12-28 09:17

    Here is the deprecated method from Configuration that still works. It is doing a lot of hibernate specific setup that hibernate users wouldn't really want to do. Things like veryifying properties and copying them from one object to another. I have also been looking for a working example of Hibernate configuration for Hibernate 4 that does not use the deprecated buildSessionFactory() method and have been unable to find one so far. I believe that the intent is to deprecate Configuration entirely.

    public SessionFactory buildSessionFactory() throws HibernateException {
        Environment.verifyProperties( properties );
        ConfigurationHelper.resolvePlaceHolders( properties );
        final ServiceRegistry serviceRegistry =  new ServiceRegistryBuilder()
                .applySettings( properties )
                .buildServiceRegistry();
        setSessionFactoryObserver(
                new SessionFactoryObserver() {
                    @Override
                    public void sessionFactoryCreated(SessionFactory factory) {
                    }
    
                    @Override
                    public void sessionFactoryClosed(SessionFactory factory) {
                        ( (StandardServiceRegistryImpl) serviceRegistry ).destroy();
                    }
                }
        );
        return buildSessionFactory( serviceRegistry );
    }
    

提交回复
热议问题