Hibernate4 buildSessionFactory过时处理办法

这几天做毕设,用到Hibernate,发现buildSessionFactory方法,上网找资料得到的答案如下:

public void test() {
		Configuration cfg = new Configuration();
		cfg.configure();		
		ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();         
		SessionFactory sf = cfg.buildSessionFactory(sr);
				
		
		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		User user = new User();
		user.setBirthday(new Date());
		user.setName("neal");
		
		session.persist(user);
		tx.commit();
		session.close();
		System.out.println("end");
	}

 但是坑爹的是ServiceRegistryBuilder也是过时的,因此可以做以下处理:

private static SessionFactory buildSessionFactory() {
	        try {
	        	Configuration configuration = new Configuration().configure(); 
	        	ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
	        	return configuration.buildSessionFactory(serviceRegistry);
	        	
	        }
	        catch (Throwable ex) {
	            System.out.println("创建SessionFactory时出错!"+ex);
	            throw new ExceptionInInitializerError(ex);
	        }
	    }

 留坑,过些天再研究Hibernate源码

 

 

你可能感兴趣的:(sessionFactory)