【问题标题】:Update empty child collection throw error更新空子集合抛出错误
【发布时间】:2016-01-11 11:00:33
【问题描述】:
class Attribute{
--- other entity def

  @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true, cascade=CascadeType.ALL)
  @JoinColumn(name="ATTRDEFID", nullable=false, insertable=false , updatable=false)
  @OrderBy("IDX")
  private List<Constraint> constraints = new ArrayList<Constraint>();  
--- other entity def
}

在更新/保存属性时,约束为空。此错误仅在更新的情况下引发。

org.springframework.orm.jpa.JpaSystemException:拥有的实体实例不再引用具有 cascade="all-delete-orphan" 的集合:com.revitas.cm.persistence.term.Attribute.constraints;嵌套异常是 org.hibernate.HibernateException:拥有的实体实例不再引用具有 cascade="all-delete-orphan" 的集合:com.revitas.cm.persistence.term.Attribute.constraints

当我设置空集合而不是null时,它保存成功。

if(attribute.getConstraints()==null){
        attribute.setConstraints(new ArrayList<>());
    }

为什么会抛出这样的错误?为什么通过空集合解决?它是否正确解决方案? 我在谷歌上搜索与this 相关的内容。但不明白为什么会出现这样的问题。

【问题讨论】:

  • 怎么变成null?你可能没有正确合并它。
  • 是的 .merge() 上不应为空。它打开了休眠问题。我回答了它,这可能对其他人有帮助。此值设置为 json 的一部分。它可以通过设置 [] 或如果为 null 则不传递元素来解决。

标签: java spring hibernate


【解决方案1】:

问题在于包含空约束并调用合并操作进行更新的属性。

根据hibernate提供的这个链接https://hibernate.atlassian.net/browse/HHH-7726

通过 parent.setChildren(null) 删除子项,然后使用 session.merge(parent) 提交更改会抛出 org.hibernate.HibernateException:拥有 cascade="all-delete-orphan" 的集合不再引用实体实例:Parent.children

如果是json,则需要设置空集合或不传递值。 Json输入会有两种方式:

1) []

2) 根本没有通过元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2015-03-27
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    相关资源
    最近更新 更多