简体   繁体   English

更新空子集合引发错误

[英]Update empty child collection throw error

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
}

At a time of update/save attribute, contraint is null. 在“更新/保存”属性时,约束为null。 This error throw in case of update only. 仅在更新时抛出此错误。

org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.revitas.cm.persistence.term.Attribute.constraints; org.springframework.orm.jpa.JpaSystemException:拥有实体实例不再引用具有cascade =“ all-delete-orphan”的集合:com.revitas.cm.persistence.term.Attribute.constraints; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.revitas.cm.persistence.term.Attribute.constraints 嵌套的异常是org.hibernate.HibernateException:拥有实体实例不再引用具有cascade =“ all-delete-orphan”的集合:com.revitas.cm.persistence.term.Attribute.constraints

When I set empty collection instead of null, it saved successfully. 当我设置空集合而不是null时,它成功保存。

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

Why such error thrown? 为什么会抛出这样的错误? why is it solved through empty collection? 为什么要通过空集合来解决? does it correct solution or not? 它是否正确解决方案? I do googling related to this . 我确实与有关。 but not getting proper idea why such issue. 但不知道为什么会这样。

The issue is with attribute which contains null constraints and called merge operation for update. 问题出在属性中,该属性包含空约束并称为合并操作以进行更新。

As per this link https://hibernate.atlassian.net/browse/HHH-7726 provided by hibernate. 按照此链接由hibernate提供https://hibernate.atlassian.net/browse/HHH-7726

removing children through parent.setChildren(null) and then committing the change using session.merge(parent) throws org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Parent.children 通过parent.setChildren(null)删除子级,然后使用session.merge(parent)提交更改,将引发org.hibernate.HibernateException:拥有实体实例不再引用具有cascade =“ all-delete-orphan”的集合:亲子

Either need to set empty collection or not passed value if json. 如果是json,则需要设置为空集合或不传递值。 Json input will be two ways: Json输入将有两种方式:

1) [] 1)[]

2) Not passed element atall. 2)根本没有通过元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM