1 21 package com.db4o.db4ounit.common.querying; 22 23 import com.db4o.*; 24 import com.db4o.config.*; 25 import com.db4o.query.*; 26 27 import db4ounit.*; 28 import db4ounit.extensions.*; 29 30 public class CascadedDeleteUpdate extends AbstractDb4oTestCase { 31 32 public static class ParentItem { 33 public Object child; 34 } 35 36 public static class ChildItem { 37 public Object parent1; 38 public Object parent2; 39 } 40 41 public static void main(String [] arguments) { 42 new CascadedDeleteUpdate().runClientServer(); 44 } 45 46 protected void configure(Configuration config) { 47 config.objectClass(ParentItem.class).cascadeOnDelete(true); 48 } 49 50 protected void store() { 51 ParentItem parentItem1 = new ParentItem(); 52 ParentItem parentItem2 = new ParentItem(); 53 54 ChildItem child = new ChildItem(); 55 child.parent1 = parentItem1; 56 child.parent2 = parentItem2; 57 parentItem1.child = child; 58 parentItem2.child = child; 59 60 db().set(parentItem1); 61 } 62 63 public void testAllObjectStored() throws Exception { 64 assertAllObjectStored(); 65 } 66 67 public void testUpdate() throws Exception { 68 Query q = newQuery(ParentItem.class); 69 ObjectSet objectSet = q.execute(); 70 while(objectSet.hasNext()){ 71 db().set(objectSet.next()); 72 } 73 db().commit(); 74 assertAllObjectStored(); 75 } 76 77 private void assertAllObjectStored() throws Exception { 78 reopen(); 79 Query q = newQuery(ParentItem.class); 80 ObjectSet objectSet = q.execute(); 81 while(objectSet.hasNext()){ 82 ParentItem parentItem = (ParentItem) objectSet.next(); 83 db().refresh(parentItem, 3); 84 Assert.isNotNull(parentItem.child); 85 } 86 } 87 88 } 89 | Popular Tags |