我在尝试更新我的实体时遇到以下问题:
"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<..>,根据这个:How to "possible" solve the problem,但它不能工作。
如果你有任何想法,请让我知道。
谢谢!
发布于 2011-04-12 00:33:09
实际上,我的问题是关于实体的equals和hashcode。遗留代码可能会带来很多问题,千万不要忘记检查它。我所做的只是保留删除-孤立策略和正确的equals和hashcode。
发布于 2011-04-08 05:29:47
选中要将某些内容分配给sonEntities的所有位置。您引用的链接明确指出要创建一个新的HashSet,但在重新分配该集合时可能会出现此错误。例如:
public void setChildren(Set<SonEntity> aSet)
{
this.sonEntities = aSet; //This will override the set that Hibernate is tracking.
}通常你只想在构造函数中“新建”一次集合。任何时候您想要添加或删除列表中的内容,您都必须修改列表的内容,而不是分配一个新的列表。
要添加子对象的:
public void addChild(SonEntity aSon)
{
this.sonEntities.add(aSon);
}用于移除子对象的:
public void removeChild(SonEntity aSon)
{
this.sonEntities.remove(aSon);
}发布于 2012-01-12 21:18:51
方法:
public void setChildren(Set<SonEntity> aSet) {
this.sonEntities = aSet;
}如果parentEntity被分离,并且如果我们更新它,则工作。
但是,如果实体没有从每个上下文中分离出来(即,查找和更新操作在同一事务中),则以下方法有效。
public void setChildren(Set<SonEntity> aSet) {
//this.sonEntities = aSet; //This will override the set that Hibernate is tracking.
this.sonEntities.clear();
if (aSet != null) {
this.sonEntities.addAll(aSet);
}
}https://stackoverflow.com/questions/5587482
复制相似问题