01: /**
02: * Copyright (C) 2001-2006 France Telecom R&D
03: */package relation.one_to_one;
04:
05: import org.objectweb.util.monolog.api.BasicLevel;
06:
07: import java.util.ArrayList;
08: import java.util.Collection;
09: import java.util.Iterator;
10: import java.util.List;
11:
12: import javax.jdo.PersistenceManager;
13: import javax.jdo.Query;
14:
15: import common.MainHelper;
16:
17: public class Main extends MainHelper {
18:
19: public static void main(String[] args) {
20: new Main().init(args).run().end();
21: }
22:
23: public MainHelper run() {
24: final int nbFoo = 4;
25: List foos = new ArrayList(nbFoo);
26: List bars = new ArrayList(nbFoo);
27: for (int i = 0; i < nbFoo; i++) {
28: Foo f = new Foo();
29: f.setFooId("foo_" + i);
30: foos.add(f);
31: Bar b = new Bar();
32: b.setBarId("bar_" + i);
33: bars.add(b);
34:
35: f.setBar(b);
36: }
37: logger.log(BasicLevel.INFO, "Create " + nbFoo
38: + "' foos and bars :" + foos + bars);
39: PersistenceManager pm = pmf.getPersistenceManager();
40: pm.currentTransaction().begin();
41: pm.makePersistentAll(foos);
42: pm.currentTransaction().commit();
43:
44: logger.log(BasicLevel.INFO, "Empty Cache.");
45: foos.clear();
46: pm.evictAll();
47:
48: pm.currentTransaction().begin();
49: Query q = pm.newQuery(Foo.class);
50: Collection c = (Collection) q.execute();
51: for (Iterator it = c.iterator(); it.hasNext();) {
52: Foo f = (Foo) it.next();
53: foos.add(f);
54: foos.add(f.getBar());
55: }
56: q.closeAll();
57: logger.log(BasicLevel.INFO, "Deleting " + foos.size()
58: + " Foos and Bars.");
59: pm.deletePersistentAll(foos);
60: pm.currentTransaction().commit();
61: pm.close();
62: return this;
63: }
64: }
|