Search in sources :

Example 31 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class SQLQueryTest method testQueryFromJdoqueryFile.

/**
 * Test of a query specified in a jdo query file.
 */
public void testQueryFromJdoqueryFile() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some inherited data to query and query using a ResultClass without constructor
        tx = pm.currentTransaction();
        tx.begin();
        Developer p3 = new Developer(13, "James", "Java", "james@java.com", (float) 15.00, "1234569", new Integer(3), "jdo");
        pm.makePersistent(p3);
        tx.commit();
        // Run the query (specified in a jdoquery file)
        tx.begin();
        Query query = pm.newNamedQuery(null, "DeveloperSkills");
        List results = (List) query.execute("jdo");
        Iterator resultsIter = results.iterator();
        while (resultsIter.hasNext()) {
            Object skill = resultsIter.next();
            LOG.debug(">> Skill : " + skill);
        }
    } catch (JDOUserException ue) {
        ue.printStackTrace();
        LOG.error(ue);
        fail("Exception thrown while persisting object and performing query : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.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) JDOUserException(javax.jdo.JDOUserException)

Example 32 with Transaction

use of javax.jdo.Transaction 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 33 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class NestedGroupsTest method testPersistReferences.

public void testPersistReferences() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        User u1 = new User("U1", "U1", "U1");
        pm.makePersistent(u1);
        User u2 = new User("U2", "U2", "U2");
        pm.makePersistent(u2);
        User u3 = new User("U3", "U3", "U3");
        pm.makePersistent(u3);
        Group g1 = new Group("G1");
        g1.getMembers().add(u1);
        pm.makePersistent(g1);
        Group g2 = new Group("G2");
        g2.getMembers().add(u2);
        pm.makePersistent(g2);
        Group g3 = new Group("G3");
        g3.getMembers().add(u3);
        pm.makePersistent(g3);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 34 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class NestedGroupsTest method testCRUD.

public void testCRUD() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        User u1 = new User("U1", "U1", "U1");
        pm.makePersistent(u1);
        User u2 = new User("U2", "U2", "U2");
        pm.makePersistent(u2);
        User u3 = new User("U3", "U3", "U3");
        pm.makePersistent(u3);
        Group g1 = new Group("G1");
        pm.makePersistent(g1);
        Group g2 = new Group("G2");
        pm.makePersistent(g2);
        Group g3 = new Group("G3");
        pm.makePersistent(g3);
        tx.commit();
        pm.close();
        // assert no relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        u1 = pm.getObjectById(User.class, "U1");
        u2 = pm.getObjectById(User.class, "U2");
        u3 = pm.getObjectById(User.class, "U3");
        g1 = pm.getObjectById(Group.class, "G1");
        g2 = pm.getObjectById(Group.class, "G2");
        g3 = pm.getObjectById(Group.class, "G3");
        assertTrue(u1.getMemberOf().isEmpty());
        assertTrue(u2.getMemberOf().isEmpty());
        assertTrue(u3.getMemberOf().isEmpty());
        assertTrue(g1.getMemberOf().isEmpty());
        assertTrue(g1.getMembers().isEmpty());
        assertTrue(g2.getMemberOf().isEmpty());
        assertTrue(g2.getMembers().isEmpty());
        assertTrue(g3.getMemberOf().isEmpty());
        assertTrue(g3.getMembers().isEmpty());
        // add some relationships
        g1.getMembers().add(u1);
        g3.getMembers().add(u3);
        g3.getMembers().add(g2);
        g2.getMembers().add(g1);
        tx.commit();
        pm.close();
        // assert new relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        u1 = pm.getObjectById(User.class, "U1");
        u2 = pm.getObjectById(User.class, "U2");
        u3 = pm.getObjectById(User.class, "U3");
        g1 = pm.getObjectById(Group.class, "G1");
        g2 = pm.getObjectById(Group.class, "G2");
        g3 = pm.getObjectById(Group.class, "G3");
        assertEquals(1, u1.getMemberOf().size());
        assertEquals(0, u2.getMemberOf().size());
        assertEquals(1, u3.getMemberOf().size());
        assertEquals(1, g1.getMemberOf().size());
        assertEquals(1, g1.getMembers().size());
        assertEquals(1, g2.getMemberOf().size());
        assertTrue(g2.getMemberOf().contains(g3));
        assertEquals(1, g2.getMembers().size());
        assertTrue(g2.getMembers().contains(g1));
        assertEquals(0, g3.getMemberOf().size());
        assertEquals(2, g3.getMembers().size());
        assertTrue(g3.getMembers().contains(u3));
        assertTrue(g3.getMembers().contains(g2));
        // change relationships
        g3.getMembers().clear();
        u2.getMemberOf().add(g2);
        tx.commit();
        // assert new relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        u1 = pm.getObjectById(User.class, "U1");
        u2 = pm.getObjectById(User.class, "U2");
        u3 = pm.getObjectById(User.class, "U3");
        g1 = pm.getObjectById(Group.class, "G1");
        g2 = pm.getObjectById(Group.class, "G2");
        g3 = pm.getObjectById(Group.class, "G3");
        assertEquals(1, u1.getMemberOf().size());
        assertEquals(1, u2.getMemberOf().size());
        assertEquals(0, u3.getMemberOf().size());
        assertEquals(1, g1.getMemberOf().size());
        assertEquals(1, g1.getMembers().size());
        assertEquals(0, g2.getMemberOf().size());
        assertEquals(2, g2.getMembers().size());
        assertTrue(g2.getMembers().contains(u2));
        assertTrue(g2.getMembers().contains(g1));
        assertEquals(0, g3.getMemberOf().size());
        assertEquals(0, g3.getMembers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 35 with Transaction

use of javax.jdo.Transaction in project tests by datanucleus.

the class OneManyTest method testOwnerAtReferencingSide.

/**
 * Department-(1)------------------------(N)-Person
 * <ul>
 * <li>The Department class has a Collection<Person> members
 * <li>In LDAP the relation is stored at the Department side (attribute members, multi-valued)
 * </ul>
 */
public void testOwnerAtReferencingSide() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // persist
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Department randd = new Department("R&D");
        randd.getMembers().add(daffyDuck);
        pm.makePersistent(daffyDuck);
        pm.makePersistent(randd);
        tx.commit();
        pm.close();
        // test fetch
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        randd = pm.getObjectById(Department.class, "R&D");
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("R&D", randd.getName());
        assertNotNull(randd.getMembers());
        assertEquals(1, randd.getMembers().size());
        assertTrue(randd.getMembers().contains(daffyDuck));
        // remove daffy duck
        randd.getMembers().remove(daffyDuck);
        tx.commit();
        pm.close();
        // test that daffy duck was removed from departement
        // ensure that daffy duck wasn't deleted
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        randd = pm.getObjectById(Department.class, "R&D");
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("R&D", randd.getName());
        assertNotNull(randd.getMembers());
        assertEquals(0, randd.getMembers().size());
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        // add duffy duck and another person
        Person speedyGonzales = new Person("Speedy", "Gonzales", "Speedy Gonzales", null, null);
        randd.getMembers().add(daffyDuck);
        randd.getMembers().add(speedyGonzales);
        tx.commit();
        pm.close();
        // test the new department members
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        randd = pm.getObjectById(Department.class, "R&D");
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        speedyGonzales = pm.getObjectById(Person.class, "Speedy Gonzales");
        assertEquals("R&D", randd.getName());
        assertNotNull(randd.getMembers());
        assertEquals(2, randd.getMembers().size());
        assertTrue(randd.getMembers().contains(daffyDuck));
        assertTrue(randd.getMembers().contains(speedyGonzales));
        // create a sales department and move daffy to it
        randd.getMembers().remove(daffyDuck);
        Department sales = new Department("Sales");
        sales.getMembers().add(daffyDuck);
        pm.makePersistent(sales);
        tx.commit();
        pm.close();
        // test the members
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        randd = pm.getObjectById(Department.class, "R&D");
        sales = pm.getObjectById(Department.class, "Sales");
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        speedyGonzales = pm.getObjectById(Person.class, "Speedy Gonzales");
        assertNotNull(randd.getMembers());
        assertEquals(1, randd.getMembers().size());
        assertTrue(randd.getMembers().contains(speedyGonzales));
        assertNotNull(sales.getMembers());
        assertEquals(1, sales.getMembers().size());
        assertTrue(sales.getMembers().contains(daffyDuck));
        // delete department and person
        pm.deletePersistent(randd);
        pm.deletePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // test deleted objects and removed relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        try {
            randd = pm.getObjectById(Department.class, "R&D");
            fail("Object 'R&D' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        try {
            daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
            fail("Object 'Daffy Duck' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        sales = pm.getObjectById(Department.class, "Sales");
        assertNotNull(sales.getMembers());
        assertEquals(0, sales.getMembers().size());
        speedyGonzales = pm.getObjectById(Person.class, "Speedy Gonzales");
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

Transaction (javax.jdo.Transaction)1552 PersistenceManager (javax.jdo.PersistenceManager)1542 Query (javax.jdo.Query)635 List (java.util.List)395 JDOUserException (javax.jdo.JDOUserException)347 Collection (java.util.Collection)287 Iterator (java.util.Iterator)216 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)183 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)156 Person (org.jpox.samples.models.company.Person)144 Employee (org.jpox.samples.models.company.Employee)122 Manager (org.jpox.samples.models.company.Manager)98 HashSet (java.util.HashSet)97 SQLException (java.sql.SQLException)80 ArrayList (java.util.ArrayList)77 JDOException (javax.jdo.JDOException)73 Extent (javax.jdo.Extent)70 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)68 JDODataStoreException (javax.jdo.JDODataStoreException)65 JDOFatalUserException (javax.jdo.JDOFatalUserException)65