Hibernate创建SessionFactory根据Hibernate版本而异

关于Hibernate buildsessionfactory()过时处理

Hibernate创建SessionFactory在Hibernate3中和Hibernate4中写法是不一样的,

在Hibernate3中写法:

static SessionFactory sessionFactroy;
static {
    try{
        Configuration config = new Configuration().configure();
        sessionFactory = config.buildSessionFactory();
    }catch(Exception e){
        e.printStackTrace();
    }    
}

在Hibernate4中写法:
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory createSessionFactory()throws HibernateException{
    Configuration configuration = newConfiguration();
    configuration.configure();
    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}

简单的的说就是

hibernate 4中用buildSessionFactory(ServiceRegistry serviceRegistry) 来代替
hibernate 3中的buildSessionFactory()

你可能感兴趣的:(SSH)