hibernate没有将数据保存到数据库

时间:2016-01-13 10:31:20

标签: java hibernate

@Override
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException {        
   Session s = NewHibernateUtil.getSessionFactory().openSession();
   Employ e = new Employ();
   e.setName("JoisCreaations");
   e.setId(2);
   Transaction t = s.beginTransaction();
   try {       
     t.begin();
     s.save(e);
     t.commit();
   }
   catch(Exception ss) {              
     t.rollback();
   }
   finally {
     s.close();
   }

   PrintWriter out = res.getWriter();
   out.println("SucesfullyAdded");

}

这是我的代码。有人能告诉我它为什么不保存数据!没有错误!!一切都很好

我甚至设置了配置文件并正确映射了所有内容

1 个答案:

答案 0 :(得分:0)

Hi Sumanth

试试这段代码。我希望这对你有用。

@Override
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException{
    SessionFactory sf = HibernateUtil.getSessionFactory();
    org.hibernate.Session ss = sf.openSession();
    Transaction tx = ss.beginTransaction();
    try {
        Employ e = new Employ();
        e.setName("JoisCreaations");
        e.setId(2);
        ss.save(e);
        tx.commit();
    } catch (Exception ee) {
        tx.rollback();
    } finally {
        ss.close();
    }
    PrintWriter out = res.getWriter();
    out.println("SucesfullyAdded");
}