java - Spring jpa hibernate insert OneToMany -


i'm trying to insert data in table (onetomany). have 2 tables(good, category).

    @entity     @table(name = "goods")     public class implements serializable {      @manytoone(fetch = fetchtype.lazy)     @joincolumn(name = "category_id")     private category category;      @jsonbackreference     public category getcategory() {         return category;     }     //getters,setters ommited 

and another:

    @entity     @table(name = "category_of_products")     public class category implements serializable {      @onetomany(mappedby = "category", fetch = fetchtype.lazy, cascade = {             cascadetype.persist, cascadetype.refresh })     private list<good> goods;      @jsonmanagedreference     public list<good> getgoods() {         return goods;     }     //getter, setters ommited 

than, in category example(id=1), trying create product relate category.

@requestmapping(value = categories_id_goods_add_do, method = requestmethod.post)         public string addgoodaction(@pathvariable("id") integer id,                 @valid good, bindingresult bindingresult, model model) {             goodvalidator.validate(good, bindingresult);             if (bindingresult.haserrors()) {                 return jspnamesutil.goods_by_category;             } else {                 resttemplate resttemplate = new resttemplate();                 category category = resttemplate.getforobject(                         "http://localhost:8080/internetshop/rest/category/" + id,                         category.class);            //here have category id                  // good.setcategory(category);                 // goodmanager.saveorupdate(good);  doesn't insert                  category.getgoods().add(good);      //get products category , add new product                 model.addattribute("good", good);                 categorymanager.saveorupdate(category); // doesn't insert             }             return jspnamesutil.goods_by_category;         } 

and don't insert in table.

hibernate: select category0_.id id1_0_0_, category0_.name name2_0_0_ category_of_products category0_ category0_.id=? hibernate: select goods0_.category_id category7_0_0_, goods0_.id id1_1_0_, goods0_.id id1_1_1_, goods0_.category_id category7_1_1_, goods0_.description descript2_1_1_, goods0_.name name3_1_1_, goods0_.price price4_1_1_, goods0_.quantity quantity5_1_1_, goods0_.short_description short_de6_1_1_ goods goods0_ goods0_.category_id=? 

method persist dont work(detach entity exception)

example of dao

public category saveorupdate(category category) {         if (category != null) {             em.merge(category);         }         return category;         }         public void add(category category) {            em.persist(category);         } 

give me please hint, wrong?

this may test solution can evolve depending on tests:

there 2 things do:

  1. change cascadetype.persist cascadetype.cascade_allto not have deal cascade problems (you can rollback this, once solution works).
  2. add addgood(good good) in category class , add (if not exists ) setter category in good class. have manage bi-directionnal relationship wiring good category , wiring category good.

the addgood method :

public void addgood(good good) {  this.goods.add(good);  good.setcategory(this); } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -