Search in sources :

Example 6 with Qualification

use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.

the class PersistenceTest method testPersistOneToOne.

/**
 * Test the persistence/retrieval of class with 1-1 relation.
 */
public void testPersistOneToOne() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qId = null;
        Object oId = null;
        try {
            tx.begin();
            Qualification q = new Qualification("Cycling Proficiency");
            Organisation o = new Organisation("BSA");
            q.setOrganisation(o);
            pm.makePersistent(q);
            tx.commit();
            qId = JDOHelper.getObjectId(q);
            oId = JDOHelper.getObjectId(o);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Qualification q = (Qualification) pm.getObjectById(qId);
            assertEquals("Cycling Proficiency", q.getName());
            Organisation o = q.getOrganisation();
            assertNotNull("Organisation is null!", o);
            assertEquals("Organisation id is different", oId, JDOHelper.getObjectId(o));
            assertEquals("Organisation name is different", "BSA", o.getName());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Qualification.class);
        clean(Organisation.class);
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager)

Example 7 with Qualification

use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.

the class JDOQLBasicTest method testCast.

public void testCast() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
        bart.addSubordinate(boss);
        bart.addSubordinate(boss2);
        Department deptB = new Department("DeptB");
        bart.addDepartment(deptB);
        Qualification q1 = new Qualification("q1");
        q1.setPerson(boss);
        Qualification q2 = new Qualification("q2");
        q2.setPerson(boss2);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(homer);
            pm.makePersistent(boss);
            pm.makePersistent(boss2);
            pm.makePersistent(q1);
            pm.makePersistent(q2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Qualification.class);
            Collection c = q.filter("((Employee)person).serialNo == \"serial 3\"").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals("q1", ((Qualification) c.iterator().next()).getName());
            q = pm.newQuery(Qualification.class);
            c = q.filter("((Manager)person).serialNo == \"serial 4\"").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals("q2", ((Qualification) c.iterator().next()).getName());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 8 with Qualification

use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.

the class JDOQLBasicTest method testQueryUsesInnerJoin.

public void testQueryUsesInnerJoin() {
    try {
        Manager woody = new Manager(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
        Department deptA = new Department("DeptA");
        Department deptB = new Department("DeptB");
        deptB.setManager(bart);
        Department deptC = new Department("DeptC");
        deptC.setManager(boss);
        // I hate the gravity
        Person coyote = new Person(5, "Wile", "E. Coyote", "wile.coyote@acme.com");
        // paranoid, and neurotic
        Person duck = new Person(6, "Daffy", "Duck", "daffy.duck@warnerbros.com");
        // You are my peanut.
        Person pepe = new Person(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com");
        // You are my peanut.
        Person pepe2 = new Person(8, "Pepe", "le Dawn", "pepe.dawn@warnerbros.com");
        Qualification qA = new Qualification("QA");
        Qualification qB = new Qualification("QB");
        qB.setPerson(duck);
        Qualification qC = new Qualification("QC");
        qC.setPerson(pepe);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // test with inheritance
            tx.begin();
            pm.makePersistent(deptC);
            pm.makePersistent(deptA);
            pm.makePersistent(deptB);
            pm.makePersistent(woody);
            pm.makePersistent(boss2);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Department.class, "manager.firstName == \"Boss\"");
            Collection c = q.executeList();
            assertEquals(c.size(), 1);
            assertEquals(((Department) c.iterator().next()).getName(), "DeptC");
            tx.commit();
            // test without inheritance
            tx.begin();
            pm.makePersistent(qC);
            pm.makePersistent(qA);
            pm.makePersistent(qB);
            pm.makePersistent(coyote);
            pm.makePersistent(pepe2);
            tx.commit();
            tx.begin();
            q = pm.newQuery(Qualification.class, "person.firstName == \"Pepe\"");
            c = q.executeList();
            assertEquals(c.size(), 1);
            assertEquals(((Qualification) c.iterator().next()).getName(), "QC");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 9 with Qualification

use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.

the class ReachabilityTest method testOneToOneUniTemporaryStates.

/**
 * Test for temporary reachability using a 1-1 unidirectional relation between 2 classes looking
 * at the object states. See JDO2 spec 12.6.7
 * See JIRA "NUCCORE-26"
 */
public void testOneToOneUniTemporaryStates() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qualId = null;
        Object orgId = null;
        try {
            tx.setOptimistic(true);
            tx.begin();
            // Create the objects of the 1-1 uni relation
            Qualification qual = new Qualification("ISO 2001 certificate number 123045");
            Organisation org = new Organisation("JPOX Corporation");
            qual.setOrganisation(org);
            // Check that both are transient
            assertTrue("Object state of new Qualification is incorrect", !JDOHelper.isPersistent(qual) && !JDOHelper.isNew(qual) && !JDOHelper.isDirty(qual));
            assertTrue("Object state of new Organisation is incorrect", !JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            // Persist the Qualification (so the Organisation should be persisted too)
            pm.makePersistent(qual);
            // Check that both are persistent-new (JDO2 spec 12.6.7)
            assertTrue("Object state of newly persisted Qualification is incorrect", JDOHelper.isPersistent(qual) && JDOHelper.isNew(qual) && JDOHelper.isDirty(qual));
            assertTrue("Object state of newly persisted (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org) && JDOHelper.isNew(org) && JDOHelper.isDirty(org));
            Organisation org2 = new Organisation("JPOX Consulting");
            qual.setOrganisation(org2);
            // Commit
            tx.commit();
            // Check that both are clean/hollow
            assertTrue("Object state of committed Qualification is incorrect", JDOHelper.isPersistent(qual) && !JDOHelper.isNew(qual) && !JDOHelper.isDirty(qual));
            assertFalse("Object state of committed (by temp reachability) Organisation is incorrect", JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            assertTrue("Object state of committed (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org2) && !JDOHelper.isNew(org2) && !JDOHelper.isDirty(org2));
            assertEquals("Field value of former persistent-new-deleted (now transient) object has been changed", "JPOX Corporation", org.getName());
            qualId = pm.getObjectId(qual);
            orgId = pm.getObjectId(org2);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the objects exist in the datastore
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query query = pm.newQuery(Organisation.class, "name == :param");
            Collection<Organisation> result = (Collection<Organisation>) query.execute("JPOX Corporation");
            Iterator<Organisation> iter = result.iterator();
            Organisation org = null;
            if (iter.hasNext()) {
                org = iter.next();
            }
            assertTrue("Organisation is in the datastore!", org == null);
            Organisation org2 = (Organisation) pm.getObjectById(orgId);
            assertTrue("Organisation 2 is not in the datastore!", org2 != null);
            Qualification qual = (Qualification) pm.getObjectById(qualId);
            assertTrue("Qualification is not in the datastore!", qual != null);
            query.closeAll();
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Qualification.class);
        clean(Organisation.class);
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection)

Example 10 with Qualification

use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.

the class ReachabilityTest method performOneToOneUniClassTemporary.

/**
 * Test for temporary reachability using a 1-1 unidirectional relation between 2 classes.
 * Tests that when persisting the owner object the initially reachable related object is NOT persisted.
 * See JDO2 spec 12.6.7
 * @param optimisticTxn Whether to use optimistic txns
 */
protected void performOneToOneUniClassTemporary(boolean optimisticTxn) {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qualId = null;
        Object orgId = null;
        try {
            tx.setOptimistic(optimisticTxn);
            tx.begin();
            // Create the objects of the 1-1 uni relation
            Qualification qual = new Qualification("ISO 2001 certificate number 123045");
            Organisation org = new Organisation("JPOX Corporation");
            qual.setOrganisation(org);
            // Check that both are transient
            assertTrue("Object state of new Qualification is incorrect", !JDOHelper.isPersistent(qual) && !JDOHelper.isNew(qual) && !JDOHelper.isDirty(qual));
            assertTrue("Object state of new Organisation is incorrect", !JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            // Persist the Qualification (so the Organisation should be persisted too)
            pm.makePersistent(qual);
            // Check that both are persistent-new (JDO2 spec 12.6.7)
            assertTrue("Object state of newly persisted Qualification is incorrect", JDOHelper.isPersistent(qual) && JDOHelper.isNew(qual) && JDOHelper.isDirty(qual));
            assertTrue("Object state of newly persisted (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org) && JDOHelper.isNew(org) && JDOHelper.isDirty(org));
            Organisation org2 = new Organisation("JPOX Consulting");
            qual.setOrganisation(org2);
            // Commit
            tx.commit();
            // Check that both are clean/hollow
            assertTrue("Object state of committed Qualification is incorrect", JDOHelper.isPersistent(qual) && !JDOHelper.isNew(qual) && !JDOHelper.isDirty(qual));
            assertFalse("Object state of committed (by temp reachability) Organisation is incorrect", JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
            assertTrue("Object state of committed (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org2) && !JDOHelper.isNew(org2) && !JDOHelper.isDirty(org2));
            qualId = pm.getObjectId(qual);
            orgId = pm.getObjectId(org2);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the objects exist in the datastore
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Organisation org = (Organisation) queryByName(pm, Organisation.class, "JPOX Corporation");
            assertTrue("Organisation is in the datastore!", org == null);
            Organisation org2 = (Organisation) pm.getObjectById(orgId);
            assertTrue("Organisation 2 is not in the datastore!", org2 != null);
            Qualification qual = (Qualification) pm.getObjectById(qualId);
            assertTrue("Qualification is not in the datastore!", qual != null);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Qualification.class);
        clean(Organisation.class);
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) Organisation(org.jpox.samples.models.company.Organisation) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)13 Transaction (javax.jdo.Transaction)13 Qualification (org.jpox.samples.models.company.Qualification)13 Organisation (org.jpox.samples.models.company.Organisation)6 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)4 Manager (org.jpox.samples.models.company.Manager)4 Collection (java.util.Collection)3 Properties (java.util.Properties)3 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)3 Query (javax.jdo.Query)3 DataStoreCache (javax.jdo.datastore.DataStoreCache)3 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)3 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)3 Department (org.jpox.samples.models.company.Department)3 Person (org.jpox.samples.models.company.Person)3 Calendar (java.util.Calendar)2 Date (java.util.Date)2 GregorianCalendar (java.util.GregorianCalendar)2 Level2Cache (org.datanucleus.cache.Level2Cache)2 StoreManager (org.datanucleus.store.StoreManager)2