Search in sources :

Example 56 with Transaction

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

the class EmbeddedTest method testUpdateOfEmbeddedObjectValues.

public void testUpdateOfEmbeddedObjectValues() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Account account = new Account("dduck", "secret12");
        Address address = new Address(12345, "D-City", "D-Street");
        ContactData contactData = new ContactData(address, null);
        contactData.getPhoneNumbers().add("+49-123-456-789");
        contactData.getPhoneNumbers().add("+49-987-654-321");
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", account, contactData);
        contactData.setPerson(daffyDuck);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // test
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("Daffy", daffyDuck.getFirstName());
        assertEquals("Duck", daffyDuck.getLastName());
        assertNotNull(daffyDuck.getAccount());
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        assertEquals("secret12", daffyDuck.getAccount().getPassword());
        assertNotNull(daffyDuck.getContactData());
        assertNotNull(daffyDuck.getContactData().getAddress());
        assertEquals(12345, daffyDuck.getContactData().getAddress().getZip());
        assertEquals("D-City", daffyDuck.getContactData().getAddress().getCity());
        assertEquals("D-Street", daffyDuck.getContactData().getAddress().getStreet());
        assertNotNull(daffyDuck.getContactData().getPhoneNumbers());
        assertEquals(2, daffyDuck.getContactData().getPhoneNumbers().size());
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-123-456-789"));
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-987-654-321"));
        assertEquals(daffyDuck, daffyDuck.getContactData().getPerson());
        // update values
        daffyDuck.getAccount().setPassword("56secret");
        daffyDuck.getContactData().getPhoneNumbers().add("+49-000-000-000");
        daffyDuck.getContactData().getAddress().setZip(23456);
        daffyDuck.getContactData().getAddress().setStreet("D-Street2");
        tx.commit();
        pm.close();
        // verify updated values
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("Daffy", daffyDuck.getFirstName());
        assertEquals("Duck", daffyDuck.getLastName());
        assertNotNull(daffyDuck.getAccount());
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        assertEquals("56secret", daffyDuck.getAccount().getPassword());
        assertNotNull(daffyDuck.getContactData());
        assertNotNull(daffyDuck.getContactData().getAddress());
        assertEquals(23456, daffyDuck.getContactData().getAddress().getZip());
        assertEquals("D-City", daffyDuck.getContactData().getAddress().getCity());
        assertEquals("D-Street2", daffyDuck.getContactData().getAddress().getStreet());
        assertNotNull(daffyDuck.getContactData().getPhoneNumbers());
        assertEquals(3, daffyDuck.getContactData().getPhoneNumbers().size());
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-123-456-789"));
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-987-654-321"));
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-000-000-000"));
        assertEquals(daffyDuck, daffyDuck.getContactData().getPerson());
        // null-out values
        daffyDuck.getAccount().setPassword(null);
        daffyDuck.getContactData().getPhoneNumbers().clear();
        daffyDuck.getContactData().getAddress().setZip(0);
        daffyDuck.getContactData().getAddress().setStreet(null);
        tx.commit();
        pm.close();
        // verify updated values
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("Daffy", daffyDuck.getFirstName());
        assertEquals("Duck", daffyDuck.getLastName());
        assertNotNull(daffyDuck.getAccount());
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        assertNull(daffyDuck.getAccount().getPassword());
        assertNotNull(daffyDuck.getContactData());
        assertNotNull(daffyDuck.getContactData().getAddress());
        assertEquals(0, daffyDuck.getContactData().getAddress().getZip());
        assertEquals("D-City", daffyDuck.getContactData().getAddress().getCity());
        assertNull(daffyDuck.getContactData().getAddress().getStreet());
        assertNotNull(daffyDuck.getContactData().getPhoneNumbers());
        assertEquals(0, daffyDuck.getContactData().getPhoneNumbers().size());
        assertEquals(daffyDuck, daffyDuck.getContactData().getPerson());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 57 with Transaction

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

the class JDQLBasicTest method testFilter.

public void testFilter() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery(Person.class);
        q.setFilter("firstName == 'Lami'");
        Collection c = (Collection) q.execute();
        assertEquals(1, c.size());
        Iterator iterator = c.iterator();
        Person lpuxa = (Person) iterator.next();
        assertEquals("Lami Puxa", lpuxa.getFullName());
        assertNotNull(lpuxa.getDepartment());
        assertEquals("Sales", lpuxa.getDepartment().getName());
        assertNotNull(lpuxa.getDepartment().getCompany());
        assertEquals("JDO Inc.", lpuxa.getDepartment().getCompany().getName());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection)

Example 58 with Transaction

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

the class ManyOneTest method testNullOutParentReferenceDetached.

public void testNullOutParentReferenceDetached() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals(2, engineering.getPersons().size());
        Person detachedBugsBunny = pm.detachCopy(bugsBunny);
        tx.commit();
        pm.close();
        // set parent reference to null
        detachedBugsBunny.setDepartment(null);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedBugsBunny);
        tx.commit();
        pm.close();
        // now check the removed child reference
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        engineering = pm.getObjectById(Department.class, "Engineering");
        assertEquals(1, engineering.getPersons().size());
        // check object doesn't exist
        try {
            pm.getObjectById(Person.class, "Bugs Bunny");
            fail("Object 'Bugs Bunny' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager)

Example 59 with Transaction

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

the class ManyOneTest method testSwapChildReference.

public void testSwapChildReference() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        Department sales = pm.getObjectById(Department.class, "Sales");
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        Person lamiPuxa = pm.getObjectById(Person.class, "Lami Puxa");
        assertEquals(2, engineering.getPersons().size());
        assertEquals(1, sales.getPersons().size());
        assertEquals(engineering, bugsBunny.getDepartment());
        assertEquals(sales, lamiPuxa.getDepartment());
        // swap references
        bugsBunny.setDepartment(sales);
        lamiPuxa.setDepartment(engineering);
        sales.getPersons().add(bugsBunny);
        engineering.getPersons().add(lamiPuxa);
        engineering.getPersons().remove(bugsBunny);
        sales.getPersons().remove(lamiPuxa);
        tx.commit();
        pm.close();
        // now check the swapped references
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        engineering = pm.getObjectById(Department.class, "Engineering");
        sales = pm.getObjectById(Department.class, "Sales");
        bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        lamiPuxa = pm.getObjectById(Person.class, "Lami Puxa");
        assertEquals(2, engineering.getPersons().size());
        assertEquals(1, sales.getPersons().size());
        assertTrue(engineering.getPersons().contains(lamiPuxa));
        assertTrue(sales.getPersons().contains(bugsBunny));
        assertEquals(engineering, lamiPuxa.getDepartment());
        assertEquals(sales, bugsBunny.getDepartment());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 60 with Transaction

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

the class ManyOneTest method testRemoveChildReferenceDetached.

public void testRemoveChildReferenceDetached() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx.begin();
        Department engineering = pm.getObjectById(Department.class, "Engineering");
        Person bugsBunny = pm.getObjectById(Person.class, "Bugs Bunny");
        assertEquals(2, engineering.getPersons().size());
        pm.getFetchPlan().setMaxFetchDepth(-1);
        Person detachedBugsBunny = pm.detachCopy(bugsBunny);
        Department detachedEngineering = pm.detachCopy(engineering);
        tx.commit();
        pm.close();
        // remove a child reference
        detachedEngineering.getPersons().remove(detachedBugsBunny);
        // detachedBugsBunny.setDepartment(null);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedEngineering);
        tx.commit();
        pm.close();
        // now check the removed child reference
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        engineering = pm.getObjectById(Department.class, "Engineering");
        assertEquals(1, engineering.getPersons().size());
        // check object doesn't exist
        try {
            pm.getObjectById(Person.class, "Bugs Bunny");
            fail("Object 'Bugs Bunny' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        tx.commit();
    } 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