EclipseLink not removing privately owned one-to-many item

963 views Asked by At

I am unable to get Eclipselink to delete a privately owned entity that is mapped as a one-to-many relationship. When I remove the privately owned entity from the one-to-many collection and set the reference to its parent to NULL and then save the parent, the privately owned entity is not removed from the database. However, it is removed from the object and during the remainder of the session, it appears that the privately owned entity was in fact removed.

Here's one mapping

@OneToMany(mappedBy="parent", cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch=FetchType.EAGER)
@PrivateOwned // should prevent orhpans, but doesn't.
List<PrivatelyOwnedEntity> privatelyOwnedEntities;

I have tried adding cascade all, and also added @CascadeOnDelete, and every combination thereof, which one would think would not have an impact, and it didn't.

otherside:

@ManyToOne
private Parent parent;

I have tried adding JoinColumn(nullable=false) and also removing the @ManyToOne annotation, and every combination of the presence/removal of these annotaitons. None of the changes had any impact.

Removal code:

public boolean removePrivatelyOwnedEntity(int i) {
    PrivatelyOwnedEntity privatelyOwnedEntityToBeRemoved = this.privatelyOwnedEntities.get(i);
    //privatelyOwnedEntityToBeRemoved.setParent(null); 
    return this.privatelyOwnedEntities.remove(privatelyOwnedEntityToBeRemoved );
}

Code that updates:

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

Save code:

public T save(T object) {
    return getJpaTemplate().merge(object);
}
1

There are 1 answers

1
James On

Is the parent part of the object's id?

Try not setting the parent to null, you should not do this for private owned objects, just remove them from the collection.