Let's see db4o's activation in action. To see activation you need a deep object-graph. To keep this example simple we create a person class with a mother-field. This allows us to simply create a very deep object graph.
First the Person class:
class Person { private Person mother; private String name; public Person(String name) { this.mother = mother; this.name = name; } public Person(Person mother, String name) { this.mother = mother; this.name = name; } public Person mother() { return mother; } public String getName() { return name; } }
After that store a deep hierarchy of persons, for example a hierarchy of seven people. Then query for it and traverse this object graph. When you hit the sixth person, that object won't be activated, because it's outside the activation depth. That object will have all fields set to null.
final Person jodie = queryForJodie(container); Person julia = jodie.mother().mother().mother().mother().mother(); // This will print null // Because julia is not activated // and therefore all fields are not set System.out.println(julia.getName()); // This will throw a NullPointerException. // Because julia is not activated // and therefore all fields are not set String joannaName = julia.mother().getName();
When you traverse deep object graphs, you might run into not activated objects. Therefore you can activate objects explicitly.
Person julia = jodie.mother().mother().mother().mother().mother(); container.activate(julia,5); System.out.println(julia.getName()); String joannaName = julia.mother().getName(); System.out.println(joannaName);
You can configure db4o to increase the activation depth. You can increase it globally or for certain classes. Or you can cascade activate certain objects.
However remember that activation is there to improve the performance and save memory. If you set the activation depth to high it will hurt the performance.
If you have a very complex model or don't want to deal with all the activation hassle then transparent activation is the best option. Transparent activation will manage the activation for you. See "Transparent Activation"
It's also possible to deactivate an object. When you deactivate an object db4o sets all its fields back to their default value and considers it as deactivated. Deactivation is useful in rare cases where you want to return to the inactivated state for some reason.
System.out.println(jodie.getName()); container.deactivate(jodie,5); // Now all fields will be null or 0 // The same applies for all references objects up to a depth of 5 System.out.println(jodie.getName());