01: /**
02: * Copyright (C) 2001-2006 France Telecom R&D
03: */package compositepk;
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 nbAddress = 5;
25: List adds = new ArrayList(nbAddress);
26: for (int i = 0; i < nbAddress; i++) {
27: Address a = new Address();
28: a.setId1(i);
29: a.setId2("address_" + i);
30: a.setStreet("street_" + i);
31: a.setTown("town_" + i);
32: a.setZip(i * 10000);
33: adds.add(a);
34: }
35: final int nbPerson = 10;
36: List persons = new ArrayList(nbPerson);
37: for (int i = 0; i < nbPerson; i++) {
38: Person p = new Person();
39: p.setFirstName("firstname_" + i);
40: p.setLastName("lastname_" + i);
41: if (i > 0) {
42: p.setFather((Person) persons.get(i - 1));
43: }
44: // find two addresses (7 is a magic number ..)
45: Collection addresses = new ArrayList();
46: addresses.add(adds.get((i * 7) % nbAddress));
47: addresses.add(adds.get(((i * 7) + 1) % nbAddress));
48: p.setAddresses(addresses);
49: persons.add(p);
50: }
51: logger.log(BasicLevel.INFO, "Create " + nbPerson
52: + "' persons :" + persons);
53: PersistenceManager pm = pmf.getPersistenceManager();
54: pm.currentTransaction().begin();
55: pm.makePersistentAll(persons);
56: pm.currentTransaction().commit();
57:
58: logger.log(BasicLevel.INFO, "Empty Cache.");
59: persons.clear();
60: adds.clear();
61: pm.evictAll();
62:
63: pm.currentTransaction().begin();
64: logger.log(BasicLevel.INFO, "Find Employees.");
65: Query q = pm.newQuery(Person.class);
66: q.setFilter("addresses.id1 == p1");
67: q.declareParameters("long p1");
68: Collection c = (Collection) q.execute(new Long(29 % nbAddress));
69: for (Iterator it = c.iterator(); it.hasNext();) {
70: Person p = (Person) it.next();
71: logger.log(BasicLevel.INFO, "- Person found:"
72: + p.toString());
73: }
74: q.closeAll();
75: pm.currentTransaction().commit();
76:
77: pm.currentTransaction().begin();
78: q = pm.newQuery(Person.class);
79: c = (Collection) q.execute();
80: persons.addAll(c);
81: q.closeAll();
82: q = pm.newQuery(Address.class);
83: c = (Collection) q.execute();
84: adds.addAll(c);
85: q.closeAll();
86: logger.log(BasicLevel.INFO, "Delete " + persons.size()
87: + " persons.");
88: pm.deletePersistentAll(persons);
89: logger.log(BasicLevel.INFO, "Delete " + adds.size()
90: + " addresses.");
91: pm.deletePersistentAll(adds);
92: pm.currentTransaction().commit();
93: pm.close();
94: return this;
95: }
96:
97: }
|