Some of the classes are not supposed to be persistent. Of course you can avoid saving their instances in your code and mark all their occurrences in another classes as transient (Java/.NET). But that needs some attention and additional coding. You can achieve the same result in an easier way using TransientClass interface:
c#:
Db4objects.Db4o.Types. ITransientClass
VB:
Db4objects.Db4o.Types. ITransientClass
TransientClass is a marker interface, which guarantees that the classes implementing it will never be added to the class metadata. In fact they are just skipped silently by db4o persistence mechanism.
An example of the TransientClass implementation is db4o object container (we do not need to save a database into itself).
Let's look how it works on an example. We will create a simplest class implementing TransientClass interface:
NotStorable.cs using Db4objects.Db4o.Types; namespace Db4objects.Db4odoc.SelectivePersistence { class NotStorable: ITransientClass { public override string ToString() { return "NotStorable class"; } } }
NotStorable.vb ' Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com Imports Db4objects.Db4o.Types Namespace Db4objects.Db4odoc.SelectivePersistence Class NotStorable Implements ITransientClass Public Overloads Overrides Function ToString() As String Return "NotStorable class" End Function End Class End Namespace
NotStorable class will be used as a field in two test objects: Test1 and Test2.
In our example we will use the default configuration and save Test1 and Test2 objects just as usual:
TransientClassExample.cs: SaveObjects private static void SaveObjects() { File.Delete(Db4oFileName); IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); try { // Save Test1 object with a NotStorable class field Test1 test1 = new Test1("Test1", new NotStorable()); container.Store(test1); // Save Test2 object with a NotStorable class field Test2 test2 = new Test2("Test2", new NotStorable(), test1); container.Store(test2); } finally { container.Close(); } }
TransientClassExample.vb: SaveObjects Public Shared Sub SaveObjects() File.Delete(Db4oFileName) Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) Try ' Save Test1 object with a NotStorable class field Dim test1 As Test1 = New Test1("Test1", New NotStorable) container.Store(test1) ' Save Test2 object with a NotStorable class field Dim test2 As Test2 = New Test2("Test2", New NotStorable, test1) container.Store(test2) Finally container.Close() End Try End Sub
Now let's try to retrieve the saved objects:
TransientClassExample.cs: RetrieveObjects private static void RetrieveObjects() { IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); try { // retrieve the results and check if the NotStorable instances were saved IList result = container.QueryByExample(null); ListResult(result); } finally { container.Close(); } }
TransientClassExample.vb: RetrieveObjects Public Shared Sub RetrieveObjects() Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) Try ' retrieve the results and check if the ' NotStorable instances were saved Dim result As IList = container.QueryByExample(Nothing) ListResult(result) Finally container.Close() End Try End SubIf you will run the example code you will see that all the instances of NotStorable class are set to null.
Download example code: