Hibernate4 buildSessionFactory过时解决方案(Annotation也是一样解决)


今天想用Hibernate4时候发现原先的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");
	}
展开阅读全文

没有更多推荐了,返回首页