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