java - Why is hibernate executing update by itself? -


i new hibernate , don't why having error. commented out dao code updating , hibernate still executing update query. here code service.

@override @transactional(readonly = false) public void updateproduct(product producttoupdate) throws duplicateproductexception {      product product = productdao.findbyproductid(producttoupdate.getproductid());      if (productdao.findbyname(producttoupdate.getname()) != null             && !product.getname().equals(producttoupdate.getname())) {         throw new duplicateproductexception();     }      product.setname(producttoupdate.getname());     product.setcategory(producttoupdate.getcategory());     product.setprice(producttoupdate.getprice());     product.setimage(producttoupdate.getimage());  //      productdao.updateproduct(product);    } 

i commented dao out , hibernate still executing query.

here code controller.

@requestmapping(value = "/updateproduct", method = requestmethod.post) public string updateproductpost(@validated @modelattribute("product") producthelper producthelper,         bindingresult bindingresult, model model) throws categorynotfoundexception {      model.addattribute("categories", categoryservice.findall());     model.addattribute("activepage", adminpage.update_product);      updateproductvalidator.validate(producthelper, bindingresult);      if (bindingresult.haserrors()) {         return "admin_home";     }      product product = producthelper.buildproducttoupdate(productservice, categoryservice);      try {          productservice.updateproduct(product);         model.addattribute("updatedproduct", product);     } catch (duplicateproductexception e) {          model.addattribute("duplicateproduct", product);     }      return "admin_home"; } 

what's weird i've got entire dao code commented out:

//  @override //  public void updateproduct(product product) { ////        sessionfactory.getcurrentsession().update(product); //  } 

and still hibernate executing update query:

hibernate: update product set category_id=?, image=?, name=?, price=?, product_id=? id=? hibernate: select product0_.id id1_3_, product0_.category_id category6_3_, product0_.image image2_3_, product0_.name name3_3_, product0_.price price4_3_, product0_.product_id product_5_3_ product product0_ product0_.product_id=? hibernate: select product0_.id id1_3_, product0_.category_id category6_3_, product0_.image image2_3_, product0_.name name3_3_, product0_.price price4_3_, product0_.product_id product_5_3_ product product0_ product0_.name=? 

if beginner mistake, sorry i'm new hibernate. thank you.

jpa uses managed entities. means whatever changes make managed entity within session persisted when session closed or flushed.

workaround can create new instance copy constructor:

product productdetached = new product(productdao.findbyproductid(producttoupdate.getproductid())) //you can change , not worry sql query productdetached.setname(producttoupdate.getname()); 

if don't want behavior, need manually detach entity.

if using jpa 2.0, can use entitymanager#detach() detach single entity persistence context. also, hibernate has session#evict() serves same purpose:

since jparepository doesn't provide functionality itself, can add custom implementation, this:

public interface userrepositorycustom {      ...    void detachuser(user u);     ... }  public interface userrepository extends jparepository<user, long>, userrepositorycustom {     ... }  @repository public class userrepositorycustomimpl implements userrepositorycustom {     ...     @persistencecontext     private entitymanager entitymanager;      @override     public void detachuser(user u) {         entitymanager.detach(u);     }     ... } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -