Hibernate 通用的添加实体方法

public void  addEntity(T entity){
          Session session = null;
          Transaction tx = null;
          try{
                   session = HibernateSessionFactory.getSession();
                   tx = session.beginTransaction();
                   session.save(entity);
                   tx.commit();
          }catch(HibernateException e){
                   if(tx != null)
                         tx.rollback();
                   throw e;
          }finally{
                   if(session != null)
                          session.close();
          }
}

你可能感兴趣的:(Hibernate 通用的添加实体方法)