01: package com.db4o.f1.chapter6;
02:
03: import com.db4o.*;
04: import com.db4o.f1.*;
05:
06: public class TranslatorExample extends Util {
07: public static void main(String[] args) {
08: tryStoreWithoutCallConstructors();
09: tryStoreWithCallConstructors();
10: storeWithTranslator();
11: }
12:
13: public static void tryStoreWithoutCallConstructors() {
14: Db4o.configure().exceptionsOnNotStorable(false);
15: Db4o.configure().objectClass(NotStorable.class)
16: .callConstructor(false);
17: tryStoreAndRetrieve();
18: }
19:
20: public static void tryStoreWithCallConstructors() {
21: Db4o.configure().exceptionsOnNotStorable(true);
22: Db4o.configure().objectClass(NotStorable.class)
23: .callConstructor(true);
24: tryStoreAndRetrieve();
25: }
26:
27: public static void storeWithTranslator() {
28: Db4o.configure().objectClass(NotStorable.class).translate(
29: new NotStorableTranslator());
30: tryStoreAndRetrieve();
31: }
32:
33: public static void tryStoreAndRetrieve() {
34: ObjectContainer db = Db4o.openFile(DB4OFILENAME);
35: try {
36: NotStorable notStorable = new NotStorable(42, "Test");
37: System.out.println("ORIGINAL: " + notStorable);
38: db.set(notStorable);
39: } catch (Exception exc) {
40: System.out.println(exc.toString());
41: return;
42: } finally {
43: db.close();
44: }
45: db = Db4o.openFile(DB4OFILENAME);
46: try {
47: ObjectSet result = db.get(NotStorable.class);
48: while (result.hasNext()) {
49: NotStorable notStorable = (NotStorable) result.next();
50: System.out.println("RETRIEVED: " + notStorable);
51: db.delete(notStorable);
52: }
53: } finally {
54: db.close();
55: }
56: }
57: }
|