/**
* Copyright (C) 2001-2006 France Telecom R&D
*/
package relation.many_to_many;
import org.objectweb.util.monolog.api.BasicLevel;
import common.MainHelper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
public class Main extends MainHelper {
public static void main(String[] args) {
new Main().init(args).run().end();
}
public MainHelper run() {
final int nbFoo = 4;
List foos = new ArrayList(nbFoo);
List bars = new ArrayList(nbFoo);
for (int i = 0; i < nbFoo; i++) {
Foo f = new Foo();
f.setMyId("foo_" + i);
Bar b = new Bar();
b.setBarId("bar_" + i);
if ((i+1) < nbFoo) {
ArrayList l = new ArrayList();
l.add(b);
if (0 < i) {
l.add(bars.get(i-1));
((Foo) foos.get(i -1)).getBars().add(b);
}
f.setBars(l);
}
foos.add(f);
bars.add(b);
}
logger.log(BasicLevel.INFO, "Create " + nbFoo + "' foos and bars :" + foos + bars);
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistentAll(foos);
pm.makePersistentAll(bars);
pm.currentTransaction().commit();
logger.log(BasicLevel.INFO, "Empty Cache.");
foos.clear();
bars.clear();
pm.evictAll();
pm.currentTransaction().begin();
Query q = pm.newQuery(Foo.class);
foos.addAll((Collection) q.execute());
q.closeAll();
q = pm.newQuery(Bar.class);
bars.addAll((Collection) q.execute());
q.closeAll();
logger.log(BasicLevel.INFO, "Deleting " + foos.size() + " Foos.");
pm.deletePersistentAll(foos);
logger.log(BasicLevel.INFO, "Deleting " + bars.size() + " Bars.");
pm.deletePersistentAll(bars);
pm.currentTransaction().commit();
pm.close();
return this;
}
}