This topic applies to Java version only
To translate NotStorable instances, we will pack their id and name values into an Object array to be stored and retrieve it from there again. Note that we don't have to do any work in onActivate(), since object reinstantiation is already fully completed in onInstantiate().
01package com.db4odoc.f1.evaluations; 02
03
import com.db4o.*; 04
import com.db4o.config.*; 05
06
public class NotStorableTranslator 07
implements ObjectConstructor { 08
public Object onStore(ObjectContainer container, 09
Object applicationObject) { 10
System.out.println("onStore for "+applicationObject); 11
NotStorable notStorable=(NotStorable)applicationObject; 12
return new Object[]{new Integer(notStorable.getId()), 13
notStorable.getName()}; 14
} 15
16
public Object onInstantiate(ObjectContainer container, 17
Object storedObject) { 18
System.out.println("onInstantiate for "+storedObject); 19
Object[] raw=(Object[])storedObject; 20
int id=((Integer)raw[0]).intValue(); 21
String name=(String)raw[1]; 22
return new NotStorable(id,name); 23
} 24
25
public void onActivate(ObjectContainer container, 26
Object applicationObject, Object storedObject) { 27
System.out.println("onActivate for "+applicationObject 28
+" / "+storedObject); 29
} 30
31
public Class storedClass() { 32
return Object[].class; 33
} 34
}
Let's try it out:
1public static void storeWithTranslator() { 2
Db4o.configure().objectClass(NotStorable.class) 3
.translate(new NotStorableTranslator()); 4
tryStoreAndRetrieve(); 5
}