[This functionality is deprecated]
db4o provides you with a possibility to create a mapping from a class in the database to a runtime class.
Java:
ObjectClass.readAs(Object clazz)
clazz
parameter specifies a runtime class,
which will be used to instantiate objects from the database.
The use-case is the following:
Java:
Db4o.configure().objectClass(A.class).readAs(B.class)
This configuration should be set before opening a database file.
The mapping functionality is similar to Aliases, but more limited.
Let's look at an example.
We will use 2 identical classes Pilot and PilotReplacement.
Objects of Pilot class will be saved to the database:
01private static void storeObjects(){ 02
new File(FILENAME).delete(); 03
ObjectContainer container = Db4o.openFile(FILENAME); 04
try { 05
Pilot pilot = new Pilot("Michael Schumacher", 100); 06
container.set(pilot); 07
pilot = new Pilot("Rubens Barichello", 99); 08
container.set(pilot); 09
} finally { 10
container.close(); 11
} 12
}
Let's try to retrieve the persisted objects using PilotReplacement class:
01private static void retrieveObjects(){ 02
Configuration configuration = Db4o.newConfiguration(); 03
configuration.objectClass(Pilot.class).readAs(PilotReplacement.class); 04
ObjectContainer container = Db4o.openFile(configuration, FILENAME); 05
try { 06
Query query = container.query(); 07
query.constrain(PilotReplacement.class); 08
ObjectSet result = query.execute(); 09
listResult(result); 10
} finally { 11
container.close(); 12
} 13
}
If meta information
for this mapping class has been stored before to the database file,
readAs
method will have no effect.