TypeAlias

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:

AliasExample.java: configureClassAlias
1public static void configureClassAlias(){ 2 // create a new alias 3 tAlias = new TypeAlias("com.db4odoc.aliases.Pilot","com.db4odoc.aliases.Driver"); 4 // add the alias to the db4o configuration 5 Db4o.configure().addAlias(tAlias); 6 // check how does the alias resolve 7 System.out.println("Stored name for com.db4odoc.aliases.Driver: "+tAlias.resolveRuntimeName("com.db4odoc.aliases.Driver")); 8 System.out.println("Runtime name for com.db4odoc.aliases.Pilot: "+tAlias.resolveStoredName("com.db4odoc.aliases.Pilot")); 9 }


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.

AliasExample.java: saveDrivers
01public static void saveDrivers(){ 02 new File(YAPFILENAME ).delete(); 03 ObjectContainer db = Db4o.openFile(YAPFILENAME); 04 try { 05 Driver driver = new Driver("David Barrichello",99); 06 db.set(driver); 07 driver = new Driver("Kimi Raikkonen",100); 08 db.set(driver); 09 } finally { 10 db.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:

AliasExample.java: removeClassAlias
1public static void removeClassAlias(){ 2 Db4o.configure().removeAlias(tAlias); 3 }
AliasExample.java: getPilots
1public static void getPilots(){ 2 ObjectContainer db = Db4o.openFile(YAPFILENAME); 3 try { 4 ObjectSet result = db.query(com.db4odoc.aliases.Pilot.class); 5 listResult(result); 6 } finally { 7 db.close(); 8 } 9 }

Obvioulsly you can install the same alias again and read the stored objects using Driver class.