Search in sources :

Example 46 with PersistenceManager

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

Example 47 with PersistenceManager

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

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

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

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

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