1. 程式人生 > >Hibernate中Sessionfactory的獲取

Hibernate中Sessionfactory的獲取

        // hibernate3.X讀取配置檔案並且獲取session物件的方式
		Configuration configuration = new Configuration().configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
        // hibernate4.X讀取配置檔案並且獲取session物件的方式
		Configuration config = new Configuration().configure("/config.xml");
		ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
		SessionFactory sessionFactory = config.buildSessionFactory(registry);
Session session = sessionFactory.openSession();
        // hibernate5.X讀取配置檔案並且獲取session物件的方式
		StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
		SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
		
Session session = sessionFactory.openSession();