Search in sources :

Example 66 with PersistenceManager

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

the class EmbeddedTest method testPersistPersonWithoutRef.

public void testPersistPersonWithoutRef() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        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());
        assertNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNull(daffyDuck.getContactData().getAddress());
        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 67 with PersistenceManager

use of javax.jdo.PersistenceManager 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 68 with PersistenceManager

use of javax.jdo.PersistenceManager 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 69 with PersistenceManager

use of javax.jdo.PersistenceManager 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 70 with PersistenceManager

use of javax.jdo.PersistenceManager 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)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65