To update structured objects in db4o, you simply call set() on them again.
1private static void UpdateCar(IObjectContainer db) 2
{ 3
IObjectSet result = db.Get(new Car("Ferrari")); 4
Car found = (Car)result.Next(); 5
found.Pilot = new Pilot("Somebody else", 0); 6
db.Set(found); 7
result = db.Get(new Car("Ferrari")); 8
ListResult(result); 9
}
[/filter]
1Private Shared Sub UpdateCar(ByVal db As IObjectContainer) 2
Dim result As IObjectSet = db.Get(New Car("Ferrari")) 3
Dim found As Car = DirectCast(result.Next(), Car) 4
found.Pilot = New Pilot("Somebody else", 0) 5
db.Set(found) 6
result = db.Get(New Car("Ferrari")) 7
ListResult(result) 8
End Sub
Let's modify the pilot, too.
1private static void UpdatePilotSingleSession(IObjectContainer db) 2
{ 3
IObjectSet result = db.Get(new Car("Ferrari")); 4
Car found = (Car)result.Next(); 5
found.Pilot.AddPoints(1); 6
db.Set(found); 7
result = db.Get(new Car("Ferrari")); 8
ListResult(result); 9
}
1Private Shared Sub UpdatePilotSingleSession(ByVal db As IObjectContainer) 2
Dim result As IObjectSet = db.Get(New Car("Ferrari")) 3
Dim found As Car = DirectCast(result.Next(), Car) 4
found.Pilot.AddPoints(1) 5
db.Set(found) 6
result = db.Get(New Car("Ferrari")) 7
ListResult(result) 8
End Sub
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:
1private static void UpdatePilotSeparateSessionsPart1(IObjectContainer db) 2
{ 3
IObjectSet result = db.Get(new Car("Ferrari")); 4
Car found = (Car)result.Next(); 5
found.Pilot.AddPoints(1); 6
db.Set(found); 7
}
1Private Shared Sub UpdatePilotSeparateSessionsPart1(ByVal db As IObjectContainer) 2
Dim result As IObjectSet = db.Get(New Car("Ferrari")) 3
Dim found As Car = DirectCast(result.Next(), Car) 4
found.Pilot.AddPoints(1) 5
db.Set(found) 6
End Sub
And in the second, we'll double-check our modification:
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.
[filter=java]
1private static Configuration updatePilotSeparateSessionsImprovedPart1() { 2
Configuration configuration = Db4o.newConfiguration(); 3
configuration.objectClass("com.db4o.f1.chapter2.Car") 4
.cascadeOnUpdate(true); 5
return configuration; 6
}
1private static IConfiguration UpdatePilotSeparateSessionsImprovedPart1() 2
{ 3
IConfiguration configuration = Db4oFactory.NewConfiguration(); 4
configuration.ObjectClass(typeof(Car)).CascadeOnUpdate(true); 5
return configuration; 6
}
[/filter]
1Private Shared Function UpdatePilotSeparateSessionsImprovedPart1(ByVal db As IObjectContainer) As IConfiguration 2
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 3
configuration.ObjectClass(GetType(Car)).CascadeOnUpdate(True) 4
Return configuration 5
End Function
You can also achieve expected results using:
c#: configuration.UpdateDepth(depth);
[filter=vb]
VB: configuration.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 and/or passed to the openFile/openClient/openServer
method.