KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > evaluations > TranslatorExample


1 package com.db4odoc.f1.evaluations;
2
3 import com.db4o.*;
4 import com.db4odoc.f1.*;
5
6 public class TranslatorExample extends Util {
7   public static void main(String JavaDoc[] args) {
8     tryStoreWithoutCallConstructors();
9     tryStoreWithCallConstructors();
10     storeWithTranslator();
11   }
12   // end main
13

14   public static void tryStoreWithoutCallConstructors() {
15         Db4o.configure().exceptionsOnNotStorable(false);
16         Db4o.configure().objectClass(NotStorable.class)
17             .callConstructor(false);
18         tryStoreAndRetrieve();
19   }
20   // end tryStoreWithoutCallConstructors
21

22   public static void tryStoreWithCallConstructors() {
23     Db4o.configure().exceptionsOnNotStorable(true);
24     Db4o.configure().objectClass(NotStorable.class)
25         .callConstructor(true);
26     tryStoreAndRetrieve();
27   }
28   // end tryStoreWithCallConstructors
29

30   public static void storeWithTranslator() {
31     Db4o.configure().objectClass(NotStorable.class)
32         .translate(new NotStorableTranslator());
33     tryStoreAndRetrieve();
34   }
35   // end storeWithTranslator
36

37   public static void tryStoreAndRetrieve() {
38     ObjectContainer db=Db4o.openFile(YAPFILENAME);
39     try {
40       NotStorable notStorable = new NotStorable(42,"Test");
41       System.out.println("ORIGINAL: "+notStorable);
42       db.set(notStorable);
43     }
44     catch(Exception JavaDoc exc) {
45       System.out.println(exc.toString());
46       return;
47     }
48     finally {
49       db.close();
50     }
51     db=Db4o.openFile(YAPFILENAME);
52     try {
53       ObjectSet result=db.get(NotStorable.class);
54       while(result.hasNext()) {
55         NotStorable notStorable=(NotStorable)result.next();
56         System.out.println("RETRIEVED: "+notStorable);
57         db.delete(notStorable);
58       }
59     }
60     finally {
61       db.close();
62     }
63   }
64   // end tryStoreAndRetrieve
65
}
Popular Tags