简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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