java - Hibernate : Transaction commited but data not inserted to the Database -


i've got unusual situation using hibernate. have shopping cart in web application. used following methods add or update cart.

isitemexists(...) - check whether item exists in cart or not

additem() - add item item not exists or update existing item.

incrementcarttotal(..) - update cart total

additem(..)

 public void addcartitem2(cartitems item, int qty) {     session ses = factory.opensession();     transaction tr = ses.begintransaction();     try {         cartitems c1 = isitemexists(item.getmodel(), item.getcart());          if (c1 != null) {             double unitprice = smodel.getstock(item.getmodel()).getunitprice();              query q = ses.createquery("update "                     + " cartitems "                     + " set qty = qty + " + qty + " , sub_total = sub_total +" + qty * unitprice                     + " model_id=" + c1.getmodel().getid()                     + "  , cart_id=" + c1.getcart().getid());             q.executeupdate();         } else {             ses.save(item);         }          tr.commit();          incrementcarttotal(item.getcart(), numberutilities.parsedouble(item.getsubtotal()));      } catch (hibernateexception e) {         e.printstacktrace();         logger.err(e.getmessage());     } {         if (ses.isopen()) {             ses.close();         }     } } 

i can add or update cart items once. after remove items cart can't add or update items cart. cart total updating. transaction commited but, data not inserted.

how can solve problem.

try this, not use additem() method update , save.

  • use isitemexists() mthod check whether item exists or not.
  • if not create method save item
  • if exists create method update existing item

save()

 public void save(cartitems item) {     session ses = factory.opensession();     try {         transaction tr = ses.begintransaction();         ses.save(item);         tr.commit();          incrementcarttotal(item.getcart(), numberutilities.parsedouble(item.getsubtotal()));     } catch (hibernateexception e) {        // handle exception     } {         // close session     } } 

update()

 public void update(cartitems item,int qty, double price) {     session ses = factory.opensession();     transaction tr = ses.begintransaction();     try {         query q = ses.createquery("update "                 + " cartitems "                 + " set qty = qty + "+qty+" , sub_total = sub_total +" + qty*price                 + " model_id=" + item.getmodel().getid()                 + "  , cart_id=" + item.getcart().getid());         q.executeupdate();          tr.commit();     } catch (hibernateexception e) {         // handle exception     } {         // close session     }     incrementcarttotal(item.getcart(), numberutilities.parsedouble(item.getsubtotal())); } 

and use these whereever want

... if(isitemexists(..)){   update(); }else{   save(); } .... 

hope helped !


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -