KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > f1 > chapter6 > TranslatorExample


1 package com.db4o.f1.chapter6;
2
3 import com.db4o.*;
4 import com.db4o.f1.*;
5
6 public class TranslatorExample extends Util {
7   public static void main(String JavaDoc[] args) {
8     tryStoreWithoutCallConstructors();
9     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)
29         .translate(new NotStorableTranslator());
30     tryStoreAndRetrieve();
31   }
32
33   public static void tryStoreAndRetrieve() {
34     ObjectContainer db=Db4o.openFile(YAPFILENAME);
35     try {
36       NotStorable notStorable = new NotStorable(42,"Test");
37       System.out.println("ORIGINAL: "+notStorable);
38       db.set(notStorable);
39     }
40     catch(Exception JavaDoc exc) {
41       System.out.println(exc.toString());
42       return;
43     }
44     finally {
45       db.close();
46     }
47     db=Db4o.openFile(YAPFILENAME);
48     try {
49       ObjectSet result=db.get(NotStorable.class);
50       while(result.hasNext()) {
51         NotStorable notStorable=(NotStorable)result.next();
52         System.out.println("RETRIEVED: "+notStorable);
53         db.delete(notStorable);
54       }
55     }
56     finally {
57       db.close();
58     }
59   }
60 }
Popular Tags