Search in sources :

Example 46 with Transaction

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

the class EmbeddedAsChildTest 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.getNotebook());
        assertNotNull(daffyDuck.getComputers());
        assertEquals(0, daffyDuck.getComputers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 47 with Transaction

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

the class EmbeddedMixedTest method testUpdateOfHierarchicalEmbedded4.

public void testUpdateOfHierarchicalEmbedded4() {
    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());
        assertEquals("Fritz", daffyDuck.getNotebook().getCards().iterator().next().getDescription());
        // update
        Set<ComputerCard> cards = daffyDuck.getNotebook().getCards();
        ComputerCard card = cards.iterator().next();
        card.setDescription("Hans");
        tx.commit();
        pm.close();
        // verify updated values
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals(1, daffyDuck.getNotebook().getCards().size());
        assertEquals("Hans", daffyDuck.getNotebook().getCards().iterator().next().getDescription());
        // verify non-upated values
        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());
        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 48 with Transaction

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

the class EmbeddedMixedTest method testUpdateOfHierarchicalEmbedded1.

public void testUpdateOfHierarchicalEmbedded1() {
    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());
        assertEquals(0, daffyDuck.getComputers().size());
        assertNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNull(daffyDuck.getContactData().getAddress());
        assertEquals(0, daffyDuck.getContactData().getPhoneNumbers().size());
        // update
        daffyDuck.getNotebook().getOperatingSystem().setVersion("XP");
        tx.commit();
        pm.close();
        // verify updated values
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("XP", 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("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        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 49 with Transaction

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

the class EmbeddedMixedTest method testUpdateEmbeddedDetached2.

public void testUpdateEmbeddedDetached2() {
    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().getOperatingSystem().setVersion("XP");
        // 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("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("XP", 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 50 with Transaction

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

the class EmbeddedMixedTest method testUpdateEmbeddedDetached9.

public void testUpdateEmbeddedDetached9() {
    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();
        detachedDaffyDuck.getNotebook().setName("Lenovo");
        detachedDaffyDuck.getNotebook().setOperatingSystem(new OperatingSystem("Ubuntu", "9.04"));
        detachedDaffyDuck.getNotebook().getCards().iterator().next().setDescription("USB Stick");
        // JDOHelper.makeDirty(detachedDaffyDuck, "notebook");
        // JDOHelper.makeDirty(detachedDaffyDuck.getNotebook(), "operatingSystem");
        // 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.getNotebook());
        assertEquals("Lenovo", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Ubuntu", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("9.04", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        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