I enclose a Netbeans project which demonstrates the problem. Cascading persist works, but cascading delete does not, leaving orphan objects.
Issue #2329: I can't get cascading delete to work in JDO
Type: Feature Request | Version: 2.7.5_03 | Priority: High | Status: Fixed | Replies: 4 |
#1
#2
Thank you for this report. Apparently cascading delete has never been implemented in ObjectDB (see also "Deleting Dependent Objects" on this old documentation page).
Build 2.7.5_04 implements cascading delete in JDO.
Your NetBeans project was converted to a minimal test case in the required format (please use this format rather than NetBeans projects for future report):
import java.util.*; import javax.jdo.*; import javax.jdo.Query; import javax.jdo.annotations.*; public class F2329 { public static void main(String[] args) { PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("objectdb:store.tmp;drop"); PersistenceManager pm = pmf.getPersistenceManager(); pm.currentTransaction().begin(); persist(pm); pm.currentTransaction().commit(); pm.currentTransaction().begin(); delete(pm); pm.currentTransaction().commit(); countInstances(pm); pm.close(); pmf.getPersistenceManager().close(); } public static void persist(PersistenceManager pm) { pm.makePersistent( new Holder("h1", new Dependent("d1"), new Dependent("d2"))); } public static void delete(PersistenceManager pm) { Query query = pm.newQuery(Holder.class); List<Holder> holders = (List<Holder>)query.execute(); pm.deletePersistentAll(holders); query.closeAll(); } public static void countInstances(PersistenceManager pm) { Query query = pm.newQuery(Holder.class); List<Holder> holders = (List<Holder>)query.execute(); System.out.println("Holders: " + holders.size()); query.closeAll(); query = pm.newQuery(Dependent.class); List<Dependent> dependents = (List<Dependent>)query.execute(); System.out.println("Dependents: " + dependents.size()); query.closeAll(); } @PersistenceCapable public static class Holder { private String id; @Persistent(dependent="true") private List<Dependent> dependents; public Holder(String id, Dependent... dependents) { this.id = id; this.dependents = Arrays.asList(dependents); } } @PersistenceCapable public static class Dependent { private String id; public Dependent(String id) { this.id = id; } } }
ObjectDB Support
#3
I had not looked at that documentation page as deletion of dependent objects as deletion of dependent elements is part of the JDO specification, so I assumed it would be implemented. Is it likely to be implemented?
#4
> Build 2.7.5_04 implements cascading delete in JDO.
Yes, as of your request it is now implemented and the test case prints:
Holders: 0 Dependents: 0
ObjectDB Support
#5
Impressive! Thank you!