Creating A Sample Test

Let's create a simple test case to check if the cascade on update setting is working as expected.

We will use a simple linked list class:

 

CascadeOnUpdate.java: Atom
01public static class Atom { 02 03 public Atom child; 04 public String name; 05 06 public Atom(){ 07 } 08 09 public Atom(Atom child){ 10 this.child = child; 11 } 12 13 public Atom(String name){ 14 this.name = name; 15 } 16 17 public Atom(Atom child, String name){ 18 this(child); 19 this.name = name; 20 } 21 }

 

 

Custom configuration should be added to configure method:

 

CascadeOnUpdate.java: configure
1protected void configure(Configuration conf) { 2 conf.objectClass(Atom.class).cascadeOnUpdate(false); 3 }

 

To prepare the database we will need to store some Atom objects:

 

CascadeOnUpdate.java: store
1protected void store() { 2 Atom atom = new Atom(new Atom(new Atom("storedGrandChild"), "storedChild"),"parent"); 3 db().set(atom); 4 db().commit(); 5 }

 

Now we are ready to test the expected behavior:

 

CascadeOnUpdate.java: test
01public void test() throws Exception { 02 Query q = newQuery(Atom.class); 03 q.descend("name").constrain("parent"); 04 ObjectSet objectSet = q.execute(); 05 Atom atom = null; 06 while (objectSet.hasNext()){ 07 // update child objects 08 atom = (Atom)objectSet.next(); 09 ((Atom)atom.child).name = "updated"; 10 ((Atom)atom.child).child.name = "notUpdated"; 11 // store the parent object 12 db().set(atom); 13 } 14 15 // commit and refresh to make sure that the changes are saved 16 // and reference cash is refreshed 17 db().commit(); 18 db().refresh(atom, Integer.MAX_VALUE); 19 20 21 q = newQuery(Atom.class); 22 q.descend("name").constrain("parent"); 23 objectSet = q.execute(); 24 while(objectSet.hasNext()){ 25 atom = (Atom) objectSet.next(); 26 Atom child = (Atom)atom.child; 27 // check if the child objects were updated 28 Assert.areEqual("updated", child.name); 29 Assert.areNotEqual("updated", child.child.name); 30 } 31 }