TypeAlias constructor accepts 2 parameters:
Note that the runtimeType class should exist in your application when you configure the alias.
The alias matches are found by comparing full names of the stored and runtime classes
Let's declare a new TypeAlias
Java:
private static TypeAlias tAlias;
The following method will initialize tAlias and configure db4o to use it:
01private static Configuration configureClassAlias(){ 02
// create a new alias 03
tAlias = new TypeAlias("com.db4odoc.aliases.Pilot","com.db4odoc.aliases.Driver"); 04
// add the alias to the db4o configuration 05
Configuration configuration = Db4o.newConfiguration(); 06
configuration.addAlias(tAlias); 07
// check how does the alias resolve 08
System.out.println("Stored name for com.db4odoc.aliases.Driver: "+tAlias.resolveRuntimeName("com.db4odoc.aliases.Driver")); 09
System.out.println("Runtime name for com.db4odoc.aliases.Pilot: "+tAlias.resolveStoredName("com.db4odoc.aliases.Pilot")); 10
return configuration; 11
}
We can always check the results of adding an alias using resolveRuntimeName and resolveStoredName as you see in the example. Basically the same methods are used internally by db4o to resolve aliased names.
01private static void saveDrivers(Configuration configuration){ 02
new File(DB4O_FILE_NAME ).delete(); 03
ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 04
try { 05
Driver driver = new Driver("David Barrichello",99); 06
container.set(driver); 07
driver = new Driver("Kimi Raikkonen",100); 08
container.set(driver); 09
} finally { 10
container.close(); 11
} 12
}
Due to the alias configured the database will have Pilot classes saved. You can check it using ObjectManager or you can remove alias and read it from the database:
1private static void removeClassAlias(Configuration configuration){ 2
configuration.removeAlias(tAlias); 3
}
1private static void getPilots(Configuration configuration){ 2
ObjectContainer container = Db4o.openFile(configuration, DB4O_FILE_NAME); 3
try { 4
ObjectSet result = container.query(com.db4odoc.aliases.Pilot.class); 5
listResult(result); 6
} finally { 7
container.close(); 8
} 9
}
Obvioulsly you can install the same alias again and read the stored objects using Driver class.