To update structured objects in db4o, you simply call set() on them again.
01public static void updateCar(ObjectContainer db) { 02
ObjectSet result=db.query(new Predicate<Car>() { 03
public boolean match(Car car){ 04
return car.getModel().equals("Ferrari"); 05
} 06
}); 07
Car found=(Car)result.next(); 08
found.setPilot(new Pilot("Somebody else",0)); 09
db.set(found); 10
result=db.query(new Predicate<Car>() { 11
public boolean match(Car car){ 12
return car.getModel().equals("Ferrari"); 13
} 14
}); 15
listResult(result); 16
}
Let's modify the pilot, too.
01public static void updatePilotSingleSession( 02
ObjectContainer db) { 03
ObjectSet result=db.query(new Predicate<Car>() { 04
public boolean match(Car car){ 05
return car.getModel().equals("Ferrari"); 06
} 07
}); 08
Car found=(Car)result.next(); 09
found.getPilot().addPoints(1); 10
db.set(found); 11
result=db.query(new Predicate<Car>() { 12
public boolean match(Car car){ 13
return car.getModel().equals("Ferrari"); 14
} 15
}); 16
listResult(result); 17
}
Nice and easy, isn't it? But there is something that is not obvious in this example. Let's see what happens if we split this task in two separate db4o sessions: In the first we modify our pilot and update his car:
01public static void updatePilotSeparateSessionsPart1( 02
ObjectContainer db) { 03
ObjectSet result=db.query(new Predicate<Car>() { 04
public boolean match(Car car){ 05
return car.getModel().equals("Ferrari"); 06
} 07
}); 08
Car found=(Car)result.next(); 09
found.getPilot().addPoints(1); 10
db.set(found); 11
}
And in the second, we'll double-check our modification:
1public static void updatePilotSeparateSessionsPart2( 2
ObjectContainer db) { 3
ObjectSet result=db.query(new Predicate<Car>() { 4
public boolean match(Car car){ 5
return car.getModel().equals("Ferrari"); 6
} 7
}); 8
listResult(result); 9
}
If you will execute this code you will see that Pilot's points are not changed What's happening here and what can we do to fix it?
Imagine a complex object with many members that have many members themselves. When updating this object, db4o would have to update all its children, grandchildren, etc. This poses a severe performance penalty and will not be necessary in most cases - sometimes, however, it will.
So, in our previous update example, we were modifying the Pilot child of a Car object. When we saved the change, we told db4o to save our Car object and assumed that the modified Pilot would be updated. But we were modifying and saving in the same manner as we were in the first update sample, so why did it work before? The first time we made the modification, db4o never actually had to retreive the modified Pilot it returned the same one that was still in memory that we modified, but it never actually updated the database. Restarting the application would show that the value was unchanged.
To be able to handle this dilemma as flexible as possible, db4o introduces the concept of update depth to control how deep an object's member tree will be traversed on update. The default update depth for all objects is 0, meaning that only primitive and String members will be updated, but changes in object members will not be reflected.
db4o provides means to control update depth with very fine granularity. For our current problem we'll advise db4o to update the full graph for Car objects by setting cascadeOnUpdate() for this class accordingly.
1public static void updatePilotSeparateSessionsImprovedPart1() { 2
Db4o.configure().objectClass("com.db4o.f1.chapter2.Car") 3
.cascadeOnUpdate(true); 4
}
01public static void updatePilotSeparateSessionsImprovedPart2( 02
ObjectContainer db) { 03
ObjectSet result=db.query(new Predicate<Car>() { 04
public boolean match(Car car){ 05
return car.getModel().equals("Ferrari"); 06
} 07
}); 08
Car found=(Car)result.next(); 09
found.getPilot().addPoints(1); 10
db.set(found); 11
}
1public static void updatePilotSeparateSessionsImprovedPart3( 2
ObjectContainer db) { 3
ObjectSet result=db.query(new Predicate<Car>() { 4
public boolean match(Car car){ 5
return car.getModel().equals("Ferrari"); 6
} 7
}); 8
listResult(result); 9
}
You can also achieve expected results using:
Java: Db4o.configure().updateDepth(depth);
However global updateDepth is not flexible enough for real-world objects having different depth of reference structures.
ATTENTION: Setting global update depth to the maximum value will result in serious performance penalty. Please, use this setting ONLY for debug purposes.
Note that container configuration must be set before the container is opened.