Search in sources :

Example 36 with Transaction

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

the class OneManyTest method testOwnerAtReferencingSideDetached.

/**
 * 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 testOwnerAtReferencingSideDetached() {
    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();
        // fetch and detach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        randd = pm.getObjectById(Department.class, "R&D");
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        Department detachedRandd = pm.detachCopy(randd);
        Person detachedDaffyDuck = pm.detachCopy(daffyDuck);
        tx.commit();
        pm.close();
        // check relationships
        assertEquals("R&D", detachedRandd.getName());
        assertNotNull(detachedRandd.getMembers());
        assertEquals(1, detachedRandd.getMembers().size());
        assertTrue(detachedRandd.getMembers().contains(detachedDaffyDuck));
        // remove daffy duck
        detachedRandd.getMembers().remove(detachedDaffyDuck);
        JDOHelper.makeDirty(detachedRandd, "members");
        // attach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedRandd);
        tx.commit();
        pm.close();
        // fetch and detach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        randd = pm.getObjectById(Department.class, "R&D");
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        detachedRandd = pm.detachCopy(randd);
        detachedDaffyDuck = pm.detachCopy(daffyDuck);
        tx.commit();
        pm.close();
        // test that daffy duck was removed from departement
        // ensure that daffy duck wasn't deleted
        assertEquals("R&D", detachedRandd.getName());
        assertNotNull(detachedRandd.getMembers());
        assertEquals(0, detachedRandd.getMembers().size());
        assertEquals("Daffy Duck", detachedDaffyDuck.getFullName());
        // add duffy duck and another person
        Person speedyGonzales = new Person("Speedy", "Gonzales", "Speedy Gonzales", null, null);
        detachedRandd.getMembers().add(detachedDaffyDuck);
        detachedRandd.getMembers().add(speedyGonzales);
        // attach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedRandd);
        tx.commit();
        pm.close();
        // fetch and detach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        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");
        detachedRandd = pm.detachCopy(randd);
        detachedDaffyDuck = pm.detachCopy(daffyDuck);
        Person detachedSpeedyGonzales = pm.detachCopy(speedyGonzales);
        tx.commit();
        pm.close();
        // test the new department members
        assertEquals("R&D", detachedRandd.getName());
        assertNotNull(detachedRandd.getMembers());
        assertEquals(2, detachedRandd.getMembers().size());
        assertTrue(detachedRandd.getMembers().contains(detachedDaffyDuck));
        assertTrue(detachedRandd.getMembers().contains(detachedSpeedyGonzales));
        // create a sales department and move daffy to it
        Department sales = new Department("Sales");
        detachedRandd.getMembers().remove(detachedDaffyDuck);
        sales.getMembers().add(detachedDaffyDuck);
        // attach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().addGroup(FetchPlan.ALL);
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedRandd);
        pm.makePersistent(sales);
        tx.commit();
        pm.close();
        // fetch and detach
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        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");
        detachedRandd = pm.detachCopy(randd);
        Department detachedSales = pm.detachCopy(sales);
        detachedDaffyDuck = pm.detachCopy(daffyDuck);
        detachedSpeedyGonzales = pm.detachCopy(speedyGonzales);
        tx.commit();
        pm.close();
        // test the members
        assertNotNull(detachedRandd.getMembers());
        assertEquals(1, detachedRandd.getMembers().size());
        assertTrue(detachedRandd.getMembers().contains(detachedSpeedyGonzales));
        assertNotNull(detachedSales.getMembers());
        assertEquals(1, detachedSales.getMembers().size());
        assertTrue(detachedSales.getMembers().contains(detachedDaffyDuck));
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    // pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 37 with Transaction

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

the class OneManyTest method testPersistWithoutRef.

public void testPersistWithoutRef() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Department randd = new Department("R&D");
        pm.makePersistent(randd);
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        pm.makePersistent(daffyDuck);
        Account dduck = new Account("dduck", "secret1");
        pm.makePersistent(dduck);
        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.getAddress());
        assertNull(daffyDuck.getComputer());
        assertNotNull(daffyDuck.getAccounts());
        assertTrue(daffyDuck.getAccounts().isEmpty());
        randd = pm.getObjectById(Department.class, "R&D");
        assertEquals("R&D", randd.getName());
        assertNotNull(randd.getMembers());
        assertTrue(randd.getMembers().isEmpty());
        dduck = pm.getObjectById(Account.class, "dduck");
        assertEquals("dduck", dduck.getUid());
        assertEquals("secret1", dduck.getPassword());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 38 with Transaction

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

the class OneManyTest method testOwnerAtReferencedSide.

/**
 * Person-(1)----------------------------(N)-Account
 * <ul>
 * <li>The Person class has a Collection<Account> accounts
 * <li>In LDAP the relation is stored at the *Account* side (attribute seeAlso, single-valued)
 * </ul>
 */
public void testOwnerAtReferencedSide() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // persist
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Account dduck = new Account("dduck", "secret1");
        daffyDuck.getAccounts().add(dduck);
        pm.makePersistent(daffyDuck);
        pm.makePersistent(dduck);
        tx.commit();
        pm.close();
        // test fetch
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        dduck = pm.getObjectById(Account.class, "dduck");
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("dduck", dduck.getUid());
        assertNotNull(daffyDuck.getAccounts());
        assertEquals(1, daffyDuck.getAccounts().size());
        assertTrue(daffyDuck.getAccounts().contains(dduck));
        // remove dduck from account list
        daffyDuck.getAccounts().remove(dduck);
        tx.commit();
        pm.close();
        // test that dduck was removed from accounts
        // ensure that dduck was deleted as it is dependent
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAccounts());
        assertEquals(0, daffyDuck.getAccounts().size());
        try {
            dduck = pm.getObjectById(Account.class, "dduck");
            fail("Object 'dduck' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        // add two accounts
        Account dduck2 = new Account("dduck2", "secret2");
        Account sgonzales2 = new Account("sgonzales2", "secret22");
        daffyDuck.getAccounts().add(dduck2);
        daffyDuck.getAccounts().add(sgonzales2);
        tx.commit();
        pm.close();
        // test the two accounts are in the account list
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setOptimistic(true);
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        dduck2 = pm.getObjectById(Account.class, "dduck2");
        sgonzales2 = pm.getObjectById(Account.class, "sgonzales2");
        assertNotNull(daffyDuck.getAccounts());
        assertEquals(2, daffyDuck.getAccounts().size());
        assertTrue(daffyDuck.getAccounts().contains(dduck2));
        assertTrue(daffyDuck.getAccounts().contains(sgonzales2));
        // create a new person and move one account to the new person
        Person speedyGonzales = new Person("Speedy", "Gonzales", "Speedy Gonzales", null, null);
        speedyGonzales.getAccounts().add(sgonzales2);
        // TODO Triggers cascade-delete since relation doesn't know of assignment!
        daffyDuck.getAccounts().remove(sgonzales2);
        pm.makePersistent(speedyGonzales);
        tx.commit();
        pm.close();
        // test new person was persisted and check account relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        speedyGonzales = pm.getObjectById(Person.class, "Speedy Gonzales");
        dduck2 = pm.getObjectById(Account.class, "dduck2");
        sgonzales2 = pm.getObjectById(Account.class, "sgonzales2");
        assertNotNull(daffyDuck.getAccounts());
        assertEquals(1, daffyDuck.getAccounts().size());
        assertTrue(daffyDuck.getAccounts().contains(dduck2));
        assertNotNull(speedyGonzales.getAccounts());
        assertEquals(1, speedyGonzales.getAccounts().size());
        assertTrue(speedyGonzales.getAccounts().contains(sgonzales2));
        // delete one person and account
        // test removed objects and relationships
        pm.deletePersistent(daffyDuck);
        pm.deletePersistent(sgonzales2);
        tx.commit();
        pm.close();
        // test that Daffy Duck and sgonzales2 were removed
        // ensure that dduck was deleted as it is dependent
        // ensure that Speedy Gonzales exists with no account
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        speedyGonzales = pm.getObjectById(Person.class, "Speedy Gonzales");
        assertNotNull(speedyGonzales.getAccounts());
        assertEquals(0, speedyGonzales.getAccounts().size());
        try {
            daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
            fail("Object 'Daffy Duck' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        try {
            dduck = pm.getObjectById(Account.class, "sgonzales2");
            fail("Object 'sgonzales2' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        try {
            dduck = pm.getObjectById(Account.class, "dduck");
            fail("Object 'dduck' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager)

Example 39 with Transaction

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

the class OneOneTest 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.getAddress());
        assertNull(daffyDuck.getComputer());
        assertNotNull(daffyDuck.getAccounts());
        assertTrue(daffyDuck.getAccounts().isEmpty());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 40 with Transaction

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

the class OneOneTest method testOwnerAtReferencingSide.

/**
 * Person-(1)----------------------------(1)-Address
 * <ul>
 * <li>The Person class has a reference to Address
 * <li>In LDAP the relation is stored at the Person side (attribute seeAlso)
 * </ul>
 */
public void testOwnerAtReferencingSide() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // persist
        tx.begin();
        Address address = new Address("D-City", "D-Street");
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", address, null);
        pm.makePersistent(daffyDuck);
        pm.makePersistent(address);
        tx.commit();
        pm.close();
        // test fetch
        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.getAddress());
        assertEquals("D-City", daffyDuck.getAddress().getCity());
        assertEquals("D-Street", daffyDuck.getAddress().getStreet());
        assertNull(daffyDuck.getComputer());
        assertNotNull(daffyDuck.getAccounts());
        assertTrue(daffyDuck.getAccounts().isEmpty());
        // set reference to null
        daffyDuck.setAddress(null);
        tx.commit();
        pm.close();
        // test removed reference
        // ensure address was deleted as it is dependent
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        address = null;
        try {
            pm.getObjectById(Address.class, "D-City");
            fail("Object 'D-City' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        assertNull(daffyDuck.getAddress());
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        // set new reference
        Address address2 = new Address("X-City", "X-Street");
        daffyDuck.setAddress(address2);
        tx.commit();
        pm.close();
        // test new reference
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAddress());
        assertEquals("X-City", daffyDuck.getAddress().getCity());
        assertEquals("X-Street", daffyDuck.getAddress().getStreet());
        // set old reference again
        address = new Address("D-City", "D-Street");
        daffyDuck.setAddress(null);
        daffyDuck.setAddress(address);
        tx.commit();
        pm.close();
        // test old reference
        // ensure second address was deleted as it is dependent
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        address2 = null;
        try {
            pm.getObjectById(Address.class, "X-City");
            fail("Object 'X-City' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        assertNotNull(daffyDuck.getAddress());
        assertEquals("D-City", daffyDuck.getAddress().getCity());
        assertEquals("D-Street", daffyDuck.getAddress().getStreet());
        // delete address
        address = pm.getObjectById(Address.class, "D-City");
        pm.deletePersistent(address);
        tx.commit();
        pm.close();
        // test object is deleted and reference is removed
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        address = null;
        try {
            pm.getObjectById(Address.class, "D-City");
            fail("Object 'D-City' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        assertNull(daffyDuck.getAddress());
        // set new reference
        Address address3 = new Address("Z-City", "Z-Street");
        daffyDuck.setAddress(address3);
        tx.commit();
        pm.close();
        // test new reference
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertNotNull(daffyDuck.getAddress());
        assertEquals("Z-City", daffyDuck.getAddress().getCity());
        assertEquals("Z-Street", daffyDuck.getAddress().getStreet());
        // delete person
        pm.deletePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // test person is deleted
        // ensure dependent address is also deleted
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = null;
        try {
            pm.getObjectById(Person.class, "Daffy Duck");
            fail("Object 'Daffy Duck' should not exist any more!");
        } catch (JDOObjectNotFoundException e) {
        // expected
        }
        try {
            pm.getObjectById(Address.class, "Z-City");
            fail("Object 'Z-City' 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