自己写了个工具类如下:public void closeSession(Session session) {
// TODO Auto-generated method stub
if(session!=null){
session.close();
}
} public void rollBackTransaction(Transaction tr) {
// TODO Auto-generated method stub
if(tr!=null){
tr.rollback();
}
} public Session getSession() {
// TODO Auto-generated method stub
return getSessionFactory().openSession();
} public SessionFactory getSessionFactory() {
    Configuration cfg = new Configuration().configure();
return cfg.buildSessionFactory();
} public Transaction getTransaction() {
// TODO Auto-generated method stub
return getSession().beginTransaction();
}上面是封装的方法下面是测试的
public static void main(String args[]){
HibernateClient o = new  HibernateClient(); 
Product po = new Product();
  po.setName("testname");
  po.setDescription("ORM Frame");
  o.saveProduct(po);
 }
public void saveProduct(Product p){
  Session session = null;
  Transaction tr = null;
  try{
  session = getSession();
  tr = getTransaction();
  session.save(p);
  tr.commit();
  }catch(HibernateException e){
  
  e.printStackTrace();
  }finally{
  session.close();
  }
很奇怪,就是存不进去,但是如果我用HibernateSessionFactory.getSession()就可以存数据。
我的getSession()方法中也是正确的啊先谢了