Search in sources :

Example 51 with Transaction

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

the class EmbeddedMixedTest method testUpdateOfEmbedded2.

public void testUpdateOfEmbedded2() {
    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());
        // assertTrue(JDOHelper.isDirty(daffyDuck));
        // assertTrue(JDOHelper.isDirty(daffyDuck.getAccount()));
        // assertTrue(JDOHelper.isDirty(daffyDuck.getContactData()));
        // assertTrue(JDOHelper.isDirty(daffyDuck.getContactData().getAddress()));
        // update values
        address = daffyDuck.getContactData().getAddress();
        address.setZip(23456);
        address.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");
        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());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 52 with Transaction

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

the class EmbeddedMixedTest method testUpdateOfHierarchicalEmbedded2.

public void testUpdateOfHierarchicalEmbedded2() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Notebook notebook = new Notebook("M-0001", "IBM", daffyDuck);
        ComputerCard isdnCard = new ComputerCard("ISDN", "Fritz");
        notebook.getCards().add(isdnCard);
        OperatingSystem os = new OperatingSystem("Windows", "Vista");
        notebook.setOperatingSystem(os);
        daffyDuck.setNotebook(notebook);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // verify
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertNotNull(daffyDuck.getNotebook());
        assertEquals("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("Vista", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        // update
        daffyDuck.getNotebook().setOperatingSystem(new OperatingSystem("Ubuntu", "9.04"));
        tx.commit();
        pm.close();
        // verify updated values
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Ubuntu", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("9.04", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        // verify non-updated values
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertEquals(0, daffyDuck.getComputers().size());
        assertNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNull(daffyDuck.getContactData().getAddress());
        assertEquals(0, daffyDuck.getContactData().getPhoneNumbers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 53 with Transaction

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

the class EmbeddedMixedTest method testUpdateEmbeddedDetached1.

public void testUpdateEmbeddedDetached1() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Account account = new Account("dduck", "secret12");
        daffyDuck.setAccount(account);
        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");
        contactData.setPerson(daffyDuck);
        daffyDuck.setContactData(contactData);
        Notebook notebook = new Notebook("M-0001", "IBM", daffyDuck);
        ComputerCard isdnCard = new ComputerCard("ISDN", "Fritz");
        notebook.getCards().add(isdnCard);
        OperatingSystem os = new OperatingSystem("Windows", "Vista");
        notebook.setOperatingSystem(os);
        daffyDuck.setNotebook(notebook);
        Computer apple = new Computer("PC-0001", "Apple");
        ComputerCard graphics = new ComputerCard("Graphics", "Matrox");
        ComputerCard sound = new ComputerCard("Sound", "Creative");
        ComputerCard network = new ComputerCard("Network", "Intel");
        apple.getCards().add(graphics);
        apple.getCards().add(sound);
        apple.getCards().add(network);
        OperatingSystem macosx = new OperatingSystem("MacOSX", "10");
        apple.setOperatingSystem(macosx);
        Computer sunfire = new Computer("PC-9999", "Sun Fire");
        OperatingSystem solaris = new OperatingSystem("Solaris", "8");
        sunfire.setOperatingSystem(solaris);
        daffyDuck.getComputers().add(apple);
        daffyDuck.getComputers().add(sunfire);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // detach
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        Person detachedDaffyDuck = pm.detachCopy(daffyDuck);
        tx.commit();
        pm.close();
        // update
        detachedDaffyDuck.getNotebook().setName("Lenovo");
        // attach the object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedDaffyDuck);
        tx.commit();
        pm.close();
        // verify
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertNotNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNotNull(daffyDuck.getContactData().getAddress());
        assertEquals(2, daffyDuck.getContactData().getPhoneNumbers().size());
        assertNotNull(daffyDuck.getNotebook());
        assertEquals("Lenovo", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("Vista", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        assertEquals(1, daffyDuck.getNotebook().getCards().size());
        assertEquals(2, daffyDuck.getComputers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 54 with Transaction

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

the class EmbeddedTest method testAttachDetach.

public void testAttachDetach() {
    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();
        // detach
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        Person detachedDaffyDuck = pm.detachCopy(daffyDuck);
        tx.commit();
        pm.close();
        // test values of detached object
        assertEquals("Daffy Duck", detachedDaffyDuck.getFullName());
        assertEquals("Daffy", detachedDaffyDuck.getFirstName());
        assertEquals("Duck", detachedDaffyDuck.getLastName());
        assertNotNull(detachedDaffyDuck.getAccount());
        assertEquals("dduck", detachedDaffyDuck.getAccount().getUid());
        assertEquals("secret12", detachedDaffyDuck.getAccount().getPassword());
        assertNotNull(detachedDaffyDuck.getContactData());
        assertNotNull(detachedDaffyDuck.getContactData().getAddress());
        assertEquals(12345, detachedDaffyDuck.getContactData().getAddress().getZip());
        assertEquals("D-City", detachedDaffyDuck.getContactData().getAddress().getCity());
        assertEquals("D-Street", detachedDaffyDuck.getContactData().getAddress().getStreet());
        assertNotNull(detachedDaffyDuck.getContactData().getPhoneNumbers());
        assertEquals(2, detachedDaffyDuck.getContactData().getPhoneNumbers().size());
        assertTrue(detachedDaffyDuck.getContactData().getPhoneNumbers().contains("+49-123-456-789"));
        assertTrue(detachedDaffyDuck.getContactData().getPhoneNumbers().contains("+49-987-654-321"));
        assertEquals(detachedDaffyDuck, detachedDaffyDuck.getContactData().getPerson());
        // update the detached object
        detachedDaffyDuck.getContactData().getAddress().setZip(23456);
        detachedDaffyDuck.getContactData().getAddress().setStreet("D-Street2");
        detachedDaffyDuck.getContactData().getPhoneNumbers().remove("+49-123-456-789");
        detachedDaffyDuck.getContactData().getPhoneNumbers().add("+49-000-000-000");
        detachedDaffyDuck.getAccount().setPassword("secret90");
        detachedDaffyDuck.setFirstName("Daffy2");
        JDOHelper.makeDirty(detachedDaffyDuck.getContactData(), "address");
        // attach the object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedDaffyDuck);
        tx.commit();
        pm.close();
        // verify that the modifications are persistent
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("Daffy2", daffyDuck.getFirstName());
        assertEquals("Duck", daffyDuck.getLastName());
        assertNotNull(daffyDuck.getAccount());
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        assertEquals("secret90", 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(2, daffyDuck.getContactData().getPhoneNumbers().size());
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-000-000-000"));
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-987-654-321"));
        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 55 with Transaction

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

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