@Test public void testSaveStudentWithOpenSession(){ //获得配置对象 Configuration config = new Configuration().configure(); //获得服务注册对象 StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build(); SessionFactory sessionFactory = config.buildSessionFactory(standardServiceRegistry); Session session = sessionFactory.openSession(); //开启事务 Transaction transaction = session.beginTransaction(); Student student = new Student("张三丰", "女", new Date(), "武当山"); session.doWork(new Work() { public void execute(Connection connection) throws SQLException { System.out.println(connection.hashCode()); } }); session.save(student); transaction.commit(); Session session2 = sessionFactory.openSession(); transaction = session2.beginTransaction(); session2.doWork(new Work() { public void execute(Connection connection) throws SQLException { System.out.println(connection.hashCode()); } }); session2.save(student); transaction.commit(); }
不是不hibernate版本问题,你的事5.x版本吧。看到用了StandardServiceRegistryBuilder