Package compositepk

Source Code of compositepk.Main

/**
* Copyright (C) 2001-2006 France Telecom R&D
*/
package compositepk;

import org.objectweb.util.monolog.api.BasicLevel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import common.MainHelper;

public class Main extends MainHelper {

    public static void main(String[] args) {
        new Main().init(args).run().end();
    }
   
    public MainHelper run() {
        final int nbAddress = 5;
        List adds = new ArrayList(nbAddress);
        for (int i = 0; i < nbAddress; i++) {
            Address a = new Address();
            a.setId1(i);
            a.setId2("address_" + i);
            a.setStreet("street_" + i);
            a.setTown("town_" + i);
            a.setZip(i * 10000);
            adds.add(a);
        }
        final int nbPerson = 10;
        List persons = new ArrayList(nbPerson);
        for (int i = 0; i < nbPerson; i++) {
            Person p = new Person();
            p.setFirstName("firstname_" + i);
            p.setLastName("lastname_" + i);
            if (i>0) {
                p.setFather((Person) persons.get(i-1));
            }
            // find two addresses (7 is a magic number ..)
            Collection addresses = new ArrayList();
            addresses.add(adds.get((i * 7) % nbAddress));
            addresses.add(adds.get(((i * 7) + 1) % nbAddress));
            p.setAddresses(addresses);
            persons.add(p);
        }
        logger.log(BasicLevel.INFO, "Create " + nbPerson + "' persons :" + persons);
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        pm.makePersistentAll(persons);
        pm.currentTransaction().commit();

        logger.log(BasicLevel.INFO, "Empty Cache.");
        persons.clear();
        adds.clear();
        pm.evictAll();
       
        pm.currentTransaction().begin();
        logger.log(BasicLevel.INFO, "Find Employees.");
        Query q = pm.newQuery(Person.class);
        q.setFilter("addresses.id1 == p1");
        q.declareParameters("long p1");
        Collection c = (Collection) q.execute(new Long(29 % nbAddress));
        for (Iterator it = c.iterator(); it.hasNext();) {
            Person p = (Person) it.next();
            logger.log(BasicLevel.INFO, "- Person found:" + p.toString());
        }
        q.closeAll();
        pm.currentTransaction().commit();
       
        pm.currentTransaction().begin();
        q = pm.newQuery(Person.class);
        c = (Collection) q.execute();
        persons.addAll(c);
        q.closeAll();
        q = pm.newQuery(Address.class);
        c = (Collection) q.execute();
        adds.addAll(c);
        q.closeAll();
        logger.log(BasicLevel.INFO, "Delete " + persons.size() + " persons.");
        pm.deletePersistentAll(persons);
        logger.log(BasicLevel.INFO, "Delete " + adds.size() + " addresses.");
        pm.deletePersistentAll(adds);
        pm.currentTransaction().commit();
        pm.close();
        return this;
    }

}
TOP

Related Classes of compositepk.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.