我有以下问题时,试图更新我的实体:
"A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance".
我有一个父实体,它有一个Set<…>的子实体。当我尝试更新它时,我得到了要设置到这个集合的所有引用并设置它。
下面的代码表示我的映射:
@OneToMany(mappedBy = "parentEntity", fetch = FetchType.EAGER)
@Cascade({ CascadeType.ALL, CascadeType.DELETE_ORPHAN })
public Set<ChildEntity> getChildren() {
return this.children;
}
我已经尝试清理Set<..只有>,根据这个:如何“可能”解决问题,但它没有工作。
如果你有什么想法,请告诉我。
谢谢!
有一个类似的bug: https://hibernate.atlassian.net/browse/HHH-9940。
以及复制它的代码:https://github.com/abenneke/sandbox/tree/master/hibernate-null-collection/src/test
有2个可能的解决方案:
集合初始化为空集合(而不是null)
orphanRemoval设置为false
例如- was:
@OneToMany(cascade = CascadeType.REMOVE,
mappedBy = "jobEntity", orphanRemoval = true)
private List<JobExecutionEntity> jobExecutionEntities;
变成:
@OneToMany(cascade = CascadeType.REMOVE,
mappedBy = "jobEntity")
private List<JobExecutionEntity> jobExecutionEntities;
有一个类似的bug: https://hibernate.atlassian.net/browse/HHH-9940。
以及复制它的代码:https://github.com/abenneke/sandbox/tree/master/hibernate-null-collection/src/test
有2个可能的解决方案:
集合初始化为空集合(而不是null)
orphanRemoval设置为false
例如- was:
@OneToMany(cascade = CascadeType.REMOVE,
mappedBy = "jobEntity", orphanRemoval = true)
private List<JobExecutionEntity> jobExecutionEntities;
变成:
@OneToMany(cascade = CascadeType.REMOVE,
mappedBy = "jobEntity")
private List<JobExecutionEntity> jobExecutionEntities;