首页 > 代码库 > hibernate4.3版本构造SessionFactory方法

hibernate4.3版本构造SessionFactory方法

hibernate3.X构造SessionFactory方法

        //读取hibernate.cfg.xml文件  
Configuration cfg = new Configuration().configure();
        //建立SessionFactory  
SessionFactory factory = cfg.buildSessionFactory();

 在hibernate4.3中函数buildSessionFactory()已经作废,以下是新的构造方法

    private static ServiceRegistry serviceRegistry = null;
    private static Configuration cfg = null;
    private static SessionFactory sessionFactory = null;

        cfg = new Configuration().configure();
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);

        //取得session  
        Session session = null;

        //开启session  
        session = sessionFactory.openSession();

 

hibernate4.3版本构造SessionFactory方法