注意操作完成后,提交事务.
H3Utils 工具类:

H3Utils 工具类
  public class H3Utils {

  private static Configuration config = null;
  private static SessionFactory sessionFactory = null;
  static {
      config = new Configuration().configure();
      sessionFactory = config.buildSessionFactory();

  }

  /**
   * 获取SessionFactory对象
   *
   * @return
   */
  public static SessionFactory getSessionFactory() {
      return sessionFactory;
  }

  /**
   * 获取session对象
   *
   * @return
   */
  public static Session getSession() {
      return sessionFactory.openSession();
  }

测试类的初始化代码:

private static Session session = null;
   private static User user = null;
   private static Transaction transaction = null;
   static{
       session = H3Utils.getSession(); 
       transaction = session.beginTransaction();
       user = new User();
       user.setId("1");
       user.setName("fy");
       user.setPwd("123");
   }  
   }

增:

@Test
       public void add(){
       //  session.save(user);
           session.persist(user);
           transaction.commit();
       }

如果有2条完全相同的数据,则会失败
        @Test
        public void delete(){

            session.delete(user);
            transaction.commit();
        }

@Test
        public void update(){

            session.update(user);
            transaction.commit();
        }

@Test
        public void select(){

            session.get(User.class,"1");
            session.load(User.class,"1");
            transaction.commit();
        }