代码如下根老师的差不多,但测试运行时分别在 创建会话工厂对象sessionFactory = config.buildSessionFactory(serviceRegistry)和释放资源时的transaction.commit()报了两个空指针异常,有高手能帮我解决一下吗
代码和错误信息贴出来啊
public class UsersTest {
private SessionFactory sessionFactory = null;
private Session session = null;
private Transaction transaction = null;
@Before
public void init(){
//1.创建配置对象
Configuration config = new Configuration().configure();
//2.创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//3.创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//4.创建会话对象
session = sessionFactory.openSession();
//5.开启事务
transaction = session.beginTransaction();
}
@Test
public void test(){
Users user = new Users(2, "root", "abcd");
session.save(user);
}
@After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
}
}