@Test public void add() { Session session = null; Transaction tx = null; try { session = HibernateUtils.getSession(); tx = session.beginTransaction(); Student student = new Student(); student.setName("王龙"); student.setAge(20); session.save(student); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } } finally { HibernateUtils.closeSession(session); } }