【问题标题】:Spring jpa hibernate insert OneToManySpring jpa休眠插入OneToMany
【发布时间】:2015-07-27 11:25:44
【问题描述】:

我正在尝试在表 (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);
        }

请给我一个提示,我做错了什么?

【问题讨论】:

  • 我已经尝试过.. java.lang.IllegalStateException:正在合并同一实体 [ua.internetshop.model.Good#1] 的多个表示。分离:[ua.internetshop.model.Good@7842bec5];分离:[ua.internetshop.model.Good@1e897800]
  • 至少有一个故障。 em.merge 操作返回的对象与作为参数传递的对象不同。所以你应该用 category = em.merge(category); 替换它。但我认为这不会解决您的问题。
  • 一个补充,我希望你清楚,当你调用merge时,hibernate不会执行查询。这只会更新将在提交时持久保存到数据库的内部表示。因此,您是否 100% 确定您在提交后检查了日志。并且完全执行了一次提交。
  • 问题是:当类别为空时,我尝试添加一些产品,一切正常。但是,当我至少有一个产品在cateory,并尝试添加新产品时,hibernate 会在新产品上更新以前的产品,而不是插入新产品。

标签: java spring hibernate rest jpa


【解决方案1】:

这可能是一个可以根据您的测试而发展的测试解决方案:

有两件事要做:

  1. 将您的CascadeType.PERSIST 更改为CascadeType.CASCADE_ALL以不必处理级联问题(一旦此解决方案有效,您可以回滚)。
  2. Category 类中添加一个addGood(Good good) 并在Good 类中添加(如果它不存在)Category 的设置器。您必须管理将Good 连接到Category 并将Category 连接回Good 的双向关系。

addGood 方法将是这样的:

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

【讨论】:

  • 感谢您的回复,但它仍然无法正常工作。当我调试时,我在双方都有适当的对象,请参阅下面的链接:gyazo.com/7e4f21cfc7f5f3e666db926bf44700e6
  • @oki 你能像你做的那样做一个截图,但我想看看该类别商品的 java id。展开category里面的goods元素和新创建的Good id。
  • 如果我理解正确,你的意思是:gyazo.com/d7e83e9613af3c97d7b13be0173c97cf
  • 事实上,在调试面板的右列值中,您的对象和 JVM id 都像这样 Good (id=117)。这就是我的意思
  • 很好,但除此之外,我需要您扩展您选择的列表中的元素?
猜你喜欢
  • 2017-08-03
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2020-11-13
相关资源
最近更新 更多