我无法让 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;
我尝试添加所有级联,还添加了@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);
}