简体   繁体   English

休眠不将数据保存到数据库

[英]hibernate not saving the data to database

@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");

}

This is my code. 这是我的代码。 Can someone tell me why it's not saving the data!!! 有人可以告诉我为什么它不保存数据!!! there are no errors!! 没有错误! and everything is fine 一切都很好

I have even setup the config file and mapped everything correctly 我什至设置了配置文件并正确映射了所有内容

Hi Sumanth 嗨苏曼思

Try this code. 试试这个代码。 I hope this will work for u. 我希望这对你有用。

@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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM