簡體   English   中英

Spring JPA Hibernate插入OneToMany

[英]Spring jpa hibernate insert OneToMany

我正在嘗試在表(OneToMany)中插入數據。 我有兩個表(好,類別)。

    @Entity
    @Table(name = "goods")
    public class Good implements Serializable {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private Category category;

    @JsonBackReference
    public Category getCategory() {
        return category;
    }
    //getters,setters is ommited

還有一個:

    @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

比方說,在類別(id = 1)中,我嘗試創建與此類別相關的產品。

@RequestMapping(value = CATEGORIES_ID_GOODS_ADD_DO, method = RequestMethod.POST)
        public String addGoodAction(@PathVariable("id") Integer id,
                @Valid Good 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 i have category by id

                // good.setCategory(category);
                // goodManager.saveOrUpdate(good);  doesn't insert anything

                category.getGoods().add(good);      //get all products from category and add new product
                model.addAttribute("good", good);
                categoryManager.saveOrUpdate(category); // doesn't insert anything
            }
            return JspNamesUtil.GOODS_BY_CATEGORY;
        }

他們沒有在我的桌子上插入任何東西。

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

方法持久化也不起作用(分離實體異常)

DAO的例子

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

給我一個提示,我做錯了什么?

這可能是一個測試解決方案,可能會根據您的測試而變化:

有兩件事要做:

  1. 將您的CascadeType.PERSIST更改為CascadeType.CASCADE_ALL不必處理級聯問題(一旦此解決方案起作用,您可以回滾此問題)。
  2. Category類中添加一個addGood(Good good)並在Good類中為Category添加一個setter(如果尚不存在)。 你必須要管理雙directionnal關系配線GoodCategory和布線回CategoryGood

addGood方法將如下所示:

public void addGood(Good good) {
 this.goods.add(good);
 good.setCategory(this);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM