我无法让Eclipselink删除映射为一对多关系的私有实体。当我从一对多集合中删除私有实体并将对其父对象的引用设置为NULL然后保存父对象时,不会从数据库中删除私有实体。但是,它将从对象中删除,并且在会话的剩余时间内,似乎实际上已删除了私有实体。
这是一个映射
@OneToMany(mappedBy="parent", cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch=FetchType.EAGER)
@PrivateOwned // should prevent orhpans, but doesn't.
List<PrivatelyOwnedEntity> privatelyOwnedEntities;
我尝试过添加cascade all,并且还添加了@CascadeOnDelete,以及它的每个组合,人们认为这些组合不会产生影响,但事实并非如此。
阿瑟赛德:
@ManyToOne
private Parent parent;
我尝试添加JoinColumn(nullable = false)并删除@ManyToOne注释,以及这些注释的存在/删除的每个组合。这些变化都没有任何影响。
删除代码:
public boolean removePrivatelyOwnedEntity(int i) {
PrivatelyOwnedEntity privatelyOwnedEntityToBeRemoved = this.privatelyOwnedEntities.get(i);
//privatelyOwnedEntityToBeRemoved.setParent(null);
return this.privatelyOwnedEntities.remove(privatelyOwnedEntityToBeRemoved );
}
更新的代码:
projectManager.saveProject(project);
//so this privatelyOwnedEntity is deep within this object.
// Its project.site.siteMap.parents.privatelyOwnedEntities.
// SiteMap is embedded in Site.
// On save it calls a dao, and the dao's abstract class handles the update
// as shown below using Springs JpaTemplate
保存代码:
public T save(T object) {
return getJpaTemplate().merge(object);
}
答案 0 :(得分:1)
是对象id的父部分吗?
尝试不将父设置为null,不应对私有对象执行此操作,只需将其从集合中删除即可。