1.为什么要用hibernateUtil这个类,先看这段代码:
      return sessionFactory;
 }
 public static Session getSession(){
  return  sessionFactory.openSession();
   }
 public Configuration configure() throws HibernateException {
  configure( "/hibernate.cfg.xml" );
  return this;
 }
会把hibernate.cfg.xml传进去,eclipse会在哪里去找这个hibernate.cfg.xml这个文件呢?会在classpath中去找这个文件。src这个目录不是classpath,但是它为什么可以找的到呢?因为scr目录最终都会编译到classpath中去。session就类似与jdbc的connection.
 static void addPerson(Person person) {
  Session session = null;
  Transaction tx = null;
  try {
   session = HibernateUtil.getSession();
   tx = session.beginTransaction();
   session.save(person);
  } catch (HibernateException e) {
   if (tx != null)
    tx.rollback();
   throw e;  //这个时候最好是把异常抛出去,因为如果只是回滚的话,就没有任何提示给调用者。注意异常的处理,如果不抛出的话,因隐藏错误
  } finally {
   session.close();
  }
原文:http://www.cnblogs.com/tian830937/p/4435944.html