Search in sources :

Example 1 with Person

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

the class SQLQueryTest method testWithoutCandidatesClassWithParameters.

/**
 * Basic test of SQL without a candidate class but with parameters.
 */
public void testWithoutCandidatesClassWithParameters() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // insert a new element for table person
        tx.begin();
        Person p = new Person(1, "Nobody", "Nobody", "nobody@jpox.org");
        pm.makePersistent(p);
        tx.commit();
        tx.begin();
        String sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ?";
        Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        List results = (List) query.execute("nobody@jpox.org");
        Iterator iter = results.iterator();
        assertEquals(1, results.size());
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj.getClass().isArray()) {
                fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
            }
            assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
            Number value = (Number) obj;
            assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
        }
        // test more than one parameter
        sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Nobody");
        iter = results.iterator();
        assertEquals(1, results.size());
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj.getClass().isArray()) {
                fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
            }
            assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
            Number value = (Number) obj;
            assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
        }
        // test more than one parameter
        sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Noboda");
        assertEquals(1, results.size());
        // test more than one parameter
        sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Nobody");
        assertEquals(1, results.size());
        // test more than one parameter
        sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Noboda");
        assertEquals(0, results.size());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while running SQL query with parameters : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 2 with Person

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

the class SQLQueryTest method testNamedParameters.

/**
 * Test use of an SQL query using named parameters (":myname1", ":myothername2", etc).
 * @throws Exception
 */
public void testNamedParameters() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // insert new elements for table person
        tx.begin();
        Person p = new Person(1, "John", "Smith", "a@jpox.org");
        pm.makePersistent(p);
        p = new Person(2, "Paul", "Smith", "b@jpox.org");
        pm.makePersistent(p);
        p = new Person(3, "Paul", "Green", "c@jpox.org");
        pm.makePersistent(p);
        p = new Person(4, "John", "Brown", "d@jpox.org");
        pm.makePersistent(p);
        p = new Person(5, "Jason", "White", "e@jpox.org");
        pm.makePersistent(p);
        p = new Person(6, "John", "White", "f@jpox.org");
        pm.makePersistent(p);
        tx.commit();
        tx.begin();
        // Use numbered params and reuse param 1
        String sqlText = "SELECT count(*) FROM PERSON WHERE (FIRSTNAME = :firstName AND LASTNAME = :surname1) OR " + "(FIRSTNAME = :firstName AND LASTNAME = :surname2)";
        Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        Map params = new HashMap();
        params.put("firstName", "John");
        params.put("surname1", "Smith");
        params.put("surname2", "Brown");
        List list = (List) query.executeWithMap(params);
        Object obj = list.iterator().next();
        if (obj instanceof Integer) {
            assertEquals(2, ((Integer) obj).intValue());
        } else if (obj instanceof Long) {
            assertEquals(2, ((Long) obj).intValue());
        } else if (obj instanceof BigInteger) {
            assertEquals(2, ((BigInteger) obj).intValue());
        } else if (obj instanceof BigDecimal) {
            assertEquals(2, ((BigDecimal) obj).intValue());
        } else {
            fail("Test doesnt support count(*) being returned as " + obj.getClass().getName());
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while running SQL query with parameters : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Person.class);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException) BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) BigInteger(java.math.BigInteger) List(java.util.List) Person(org.jpox.samples.models.company.Person) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Person

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

the class SQLQueryTest method testWithCandidateClassWithComments.

/**
 * Test of a query with a candidate class with comments in the SELECT.
 * @throws Exception Thrown if an error occurs
 */
public void testWithCandidateClassWithComments() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some basic data to query
        tx.begin();
        Person p1 = new Person(1, "First", "Person", "first.person@jpox.org");
        pm.makePersistent(p1);
        Person p2 = new Person(2, "Second", "Person", "second.person@jpox.org");
        pm.makePersistent(p2);
        tx.commit();
        // Query for a basic object, including the PK field(s) and a comment
        tx = pm.currentTransaction();
        tx.begin();
        String queryStr = null;
        if (pmf.getMetadata(Person.class.getName()).getIdentityType().equals(IdentityType.APPLICATION)) {
            queryStr = "SELECT /*SomethingOrOther*/PERSONNUM, GLOBALNUM FROM PERSON WHERE FIRSTNAME = 'Second'";
        } else {
            queryStr = "SELECT /*SomethingOrOther*/PERSON_ID FROM PERSON WHERE FIRSTNAME = 'Second'";
        }
        Query query = pm.newQuery("javax.jdo.query.SQL", queryStr);
        query.setClass(Person.class);
        List<Person> results = (List<Person>) query.execute();
        assertNotNull(results);
        assertEquals("Number of results incorrect", 1, results.size());
        Person p = results.iterator().next();
        assertTrue("Query with candidate class has returned the wrong Person object.", p.getPersonNum() == 2);
        assertTrue("Query with candidate class has returned the wrong Person object.", p.getFirstName().equals("Second"));
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while performing SQL query using candidate class : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 4 with Person

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

the class SQLQueryTest method testWithCandidateClassWithoutResultClass.

/**
 * Test of a query with a candidate class, without a result class.
 * @throws Exception Thrown if an error occurs
 */
public void testWithCandidateClassWithoutResultClass() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some basic data to query
        tx.begin();
        Person p1 = new Person(1, "First", "Person", "first.person@jpox.org");
        pm.makePersistent(p1);
        Person p2 = new Person(2, "Second", "Person", "second.person@jpox.org");
        pm.makePersistent(p2);
        tx.commit();
        // Query for a basic object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query query = pm.newNamedQuery(Person.class, "PeopleWithEmail");
        List results = (List) query.execute("second.person@jpox.org");
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been Person", obj.getClass().getName().equals(Person.class.getName()));
            Person p = (Person) obj;
            assertTrue("Query with candidate class has returned the wrong Person object.", p.getPersonNum() == 2);
            assertTrue("Query with candidate class has returned the wrong Person object.", p.getFirstName().equals("Second"));
        }
        tx.commit();
        // Create some inherited data to query
        tx = pm.currentTransaction();
        tx.begin();
        Developer p3 = new Developer(10, "James", "Java", "james@java.com", (float) 12.00, "1234567", new Integer(1), "jdo");
        pm.makePersistent(p3);
        tx.commit();
        // Query for an inherited object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery = pm.newNamedQuery(Developer.class, "DeveloperWithSkill");
        results = (List) inhQuery.execute("jdo");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been Developer", obj.getClass().getName().equals(Developer.class.getName()));
            Developer p = (Developer) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
        }
        tx.commit();
        // Create some inherited data to query
        tx = pm.currentTransaction();
        tx.begin();
        Developer p4 = new Developer(11, "Paul", "Perl", "paul@perl.com", (float) 6.00, "1234568", new Integer(2), "perl");
        p4.setGlobalNum("GUID-p4");
        pm.makePersistent(p4);
        tx.commit();
        // Query for an inherited object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery2 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoin");
        results = (List) inhQuery2.execute("perl");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been Developer", obj.getClass().getName().equals(Developer.class.getName()));
            Developer p = (Developer) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("perl"));
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getGlobalNum().equals("GUID-p4"));
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while performing SQL query using candidate class : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
        clean(Person.class);
    }
}
Also used : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Developer(org.jpox.samples.models.company.Developer) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 5 with Person

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

the class ApplicationIdPersistenceTest method testOneToOne.

public void testOneToOne() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object p1Id = null;
        Object p2Id = null;
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            p2.setBestFriend(p1);
            pm.makePersistent(p2);
            tx.commit();
            p1Id = pm.getObjectId(p1);
            p2Id = pm.getObjectId(p2);
        } catch (Exception e) {
            LOG.error("Exception during 1-1 persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // No L2 cache interference
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Check number of objects present
            Query q = pm.newQuery(Person.class);
            List<Person> results = (List<Person>) q.execute();
            assertEquals(2, results.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-1 retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = null;
            try {
                p1 = (Person) pm.getObjectById(p1Id);
                assertEquals("Bugs", p1.getFirstName());
                assertEquals("Bunny", p1.getLastName());
                assertEquals(1, p1.getPersonNum());
            } catch (JDOObjectNotFoundException onfe) {
                fail("Person p1 not found");
            }
            assertNull("p1 best friend should be null", p1.getBestFriend());
            Person p2 = null;
            try {
                p2 = (Person) pm.getObjectById(p2Id);
                assertEquals("Daffy", p2.getFirstName());
                assertEquals("Duck", p2.getLastName());
                assertEquals(2, p2.getPersonNum());
            } catch (JDOObjectNotFoundException onfe) {
                fail("Person p2 not found");
            }
            assertNotNull("p2 best friend should not be null", p2.getBestFriend());
            assertEquals("p2 best friend should be same as p1 object", p1, p2.getBestFriend());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-1 retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

Person (org.jpox.samples.models.company.Person)132 PersistenceManager (javax.jdo.PersistenceManager)130 Transaction (javax.jdo.Transaction)128 Query (javax.jdo.Query)75 List (java.util.List)60 JDOUserException (javax.jdo.JDOUserException)41 Iterator (java.util.Iterator)40 Collection (java.util.Collection)27 Employee (org.jpox.samples.models.company.Employee)22 HashMap (java.util.HashMap)21 JDODataStoreException (javax.jdo.JDODataStoreException)21 JDOException (javax.jdo.JDOException)16 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)16 ArrayList (java.util.ArrayList)15 BigInteger (java.math.BigInteger)14 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)14 BigDecimal (java.math.BigDecimal)10 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)10 JDOQuery (org.datanucleus.api.jdo.JDOQuery)10 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)10