Search in sources :

Example 11 with Animal

use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.

the class ManagedRelationshipTest method testOneToManyFKBidirDeleteElement.

/**
 * Test for management of relations with a 1-N FK bidir where an element is deleted and we check that
 * the collection is correctly updated.
 */
public void testOneToManyFKBidirDeleteElement() {
    try {
        // Persist the objects so we have them
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
        Transaction tx = pm.currentTransaction();
        Object farmId = null;
        Object animal1Id = null;
        Object animal2Id = null;
        try {
            tx.begin();
            Farm farm = new Farm("Giles Farm");
            Animal animal1 = new Animal("Cow");
            Animal animal2 = new Animal("Dog");
            farm.addAnimal(animal1);
            farm.addAnimal(animal2);
            animal1.setFarm(farm);
            animal2.setFarm(farm);
            pm.makePersistent(farm);
            tx.commit();
            farmId = pm.getObjectId(farm);
            animal1Id = pm.getObjectId(animal1);
            animal2Id = pm.getObjectId(animal2);
        } catch (Exception e) {
            LOG.error("Exception thrown persisting 1-N FK bidir", e);
            fail("Error in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the objects and delete an element
        pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Farm farm = (Farm) pm.getObjectById(farmId);
            Animal animal1 = (Animal) pm.getObjectById(animal1Id);
            Animal animal2 = (Animal) pm.getObjectById(animal2Id);
            assertEquals("Animal1 'farm' is incorrect at retrieve", farm, animal1.getFarm());
            assertEquals("Animal2 'farm' is incorrect at retrieve", farm, animal2.getFarm());
            assertEquals("Farm has incorrect number of animals at retrieve", 2, farm.getAnimals().size());
            // Delete Animal1
            pm.deletePersistent(animal1);
            pm.flush();
            assertEquals("Farm has incorrect number of animals after animal delete", 1, farm.getAnimals().size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown deleting element from 1-N FK bidir", e);
            fail("Error in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) Farm(org.jpox.samples.one_many.bidir.Farm)

Example 12 with Animal

use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.

the class ManagedRelationshipTest method testOneToManyFKBidirPersistCollection.

/**
 * Test for management of relations with a 1-N FK bidir where the objects are persisted
 * and only the collection side is set.
 */
public void testOneToManyFKBidirPersistCollection() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Farm farm = new Farm("Giles Farm");
            Animal animal1 = new Animal("Cow");
            Animal animal2 = new Animal("Dog");
            farm.addAnimal(animal1);
            farm.addAnimal(animal2);
            assertNull("Animal1 has non-null Farm but should be null before persist", animal1.getFarm());
            assertNull("Animal2 has non-null Farm but should be null before persist", animal2.getFarm());
            assertNotNull("Farm has null Animals yet should be non-null before persist", farm.getAnimals());
            pm.makePersistent(farm);
            pm.flush();
            // Check that the relation sides are both set
            assertNotNull("Animal1 has null Farm but should be non-null after persist/flush", animal1.getFarm());
            assertNotNull("Animal2 has null Farm but should be non-null after persist/flush", animal2.getFarm());
            assertEquals("Farm has incorrect Animals after persist/flush", 2, farm.getAnimals().size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown persisting 1-N FK bidir with only collection side set", e);
            fail("Error in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) Farm(org.jpox.samples.one_many.bidir.Farm)

Example 13 with Animal

use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.

the class ManagedRelationshipTest method testOneToManyFKBidirUpdateElement.

/**
 * Test for management of relations with a 1-N FK bidir where an element is updated to be in a different
 * owner collection and we check that the collections are correctly updated.
 */
public void testOneToManyFKBidirUpdateElement() {
    try {
        // Persist the objects so we have them
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
        Transaction tx = pm.currentTransaction();
        Object farm1Id = null;
        Object farm2Id = null;
        Object animal1Id = null;
        Object animal2Id = null;
        try {
            tx.begin();
            Farm farm1 = new Farm("Giles Farm");
            Farm farm2 = new Farm("Sunnybrook Farm");
            Animal animal1 = new Animal("Cow");
            Animal animal2 = new Animal("Dog");
            farm1.addAnimal(animal1);
            farm1.addAnimal(animal2);
            animal1.setFarm(farm1);
            animal2.setFarm(farm1);
            pm.makePersistent(farm1);
            pm.makePersistent(farm2);
            tx.commit();
            farm1Id = pm.getObjectId(farm1);
            farm2Id = pm.getObjectId(farm2);
            animal1Id = pm.getObjectId(animal1);
            animal2Id = pm.getObjectId(animal2);
        } catch (Exception e) {
            LOG.error("Exception thrown persisting 1-N FK bidir", e);
            fail("Error in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the objects and update an element
        pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Farm farm1 = (Farm) pm.getObjectById(farm1Id);
            Farm farm2 = (Farm) pm.getObjectById(farm2Id);
            Animal animal1 = (Animal) pm.getObjectById(animal1Id);
            Animal animal2 = (Animal) pm.getObjectById(animal2Id);
            assertEquals("Animal1 'farm' is incorrect at retrieve", farm1, animal1.getFarm());
            assertEquals("Animal2 'farm' is incorrect at retrieve", farm1, animal2.getFarm());
            assertEquals("Farm1 has incorrect number of animals at retrieve", 2, farm1.getAnimals().size());
            assertEquals("Farm2 has incorrect number of animals at retrieve", 0, farm2.getAnimals().size());
            // Make sure the animals are loaded (cached)
            farm1.getAnimals().iterator();
            farm2.getAnimals().iterator();
            // Move Animal1 from Farm1 to Farm2
            animal1.setFarm(farm2);
            pm.flush();
            assertEquals("Farm1 has incorrect number of animals after animal update", 1, farm1.getAnimals().size());
            assertEquals("Farm2 has incorrect number of animals after animal update", 1, farm2.getAnimals().size());
            assertFalse("House1 contains window1 after update but shouldnt", farm1.getAnimals().contains(animal1));
            assertTrue("House2 doesnt contain window1 after update but should", farm2.getAnimals().contains(animal1));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown updating element from 1-N FK bidir", e);
            fail("Error in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the objects and check
        pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Farm farm1 = (Farm) pm.getObjectById(farm1Id);
            Farm farm2 = (Farm) pm.getObjectById(farm2Id);
            Animal animal1 = (Animal) pm.getObjectById(animal1Id);
            Animal animal2 = (Animal) pm.getObjectById(animal2Id);
            assertEquals("Animal1 'farm' is incorrect at retrieve", farm2, animal1.getFarm());
            assertEquals("Animal2 'farm' is incorrect at retrieve", farm1, animal2.getFarm());
            assertEquals("Farm1 has incorrect number of animals at retrieve", 1, farm1.getAnimals().size());
            assertEquals("Farm2 has incorrect number of animals at retrieve", 1, farm2.getAnimals().size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown updating element from 1-N FK bidir", e);
            fail("Error in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) Farm(org.jpox.samples.one_many.bidir.Farm)

Example 14 with Animal

use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.

the class ManagedRelationshipTest method testOneToManyFKBidirSetCollectionMoveElement.

/**
 * Test for management of relations with a 1-N FK bidir where an element is being moved from one collection owner to another,
 * by setting a collection containing that element on a new owner.
 * The implication of the test is that secondary changes will also be handled by the RelationshipManager (i.e if an element is added to
 * one collection, and is using FK, then has to also be removed from the old collection (in datastore and in memory).
 */
@TransactionMode(PESSIMISTIC)
public // FIXME Fix text in Optimistic mode
void testOneToManyFKBidirSetCollectionMoveElement() {
    PersistenceManager pm = null;
    Transaction tx = null;
    try {
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        // Create object graph:
        // farm1 <-> {animal1, animal2}
        // farm2 <-> {animal3, animal4}
        // farm5 <-> {animal5}
        tx.begin();
        Farm farm1 = new Farm("farm1");
        Animal animal1 = new Animal("animal1");
        Animal animal2 = new Animal("animal2");
        farm1.setAnimals(createSet(animal1, animal2));
        pm.makePersistent(farm1);
        Farm farm2 = new Farm("farm2");
        Animal animal3 = new Animal("animal3");
        Animal animal4 = new Animal("animal4");
        farm2.setAnimals(createSet(animal3, animal4));
        pm.makePersistent(farm2);
        Farm farm3 = new Farm("farm3");
        Animal animal5 = new Animal("animal5");
        farm3.setAnimals(createSet(animal5));
        pm.makePersistent(farm3);
        pm.flush();
        // validate objectgraph
        assertEquals(createSet(animal1, animal2), farm1.getAnimals());
        assertEquals(farm1, animal1.getFarm());
        assertEquals(farm1, animal2.getFarm());
        assertEquals(createSet(animal3, animal4), farm2.getAnimals());
        assertEquals(farm2, animal3.getFarm());
        assertEquals(farm2, animal4.getFarm());
        assertEquals(createSet(animal5), farm3.getAnimals());
        assertEquals(farm3, animal5.getFarm());
        pm.flush();
        // perform update and validate
        LOG.info(">> farm1 " + farm1 + " id=" + JDOHelper.getObjectId(farm1) + " state=" + JDOHelper.getObjectState(farm1));
        LOG.info(">> farm2 " + farm2 + " id=" + JDOHelper.getObjectId(farm2) + " state=" + JDOHelper.getObjectState(farm2));
        LOG.info(">> farm3 " + farm3 + " id=" + JDOHelper.getObjectId(farm3) + " state=" + JDOHelper.getObjectState(farm3));
        LOG.info(">> animal1 " + animal1 + " id=" + JDOHelper.getObjectId(animal1) + " farm=" + JDOHelper.getObjectId(animal1.getFarm()));
        LOG.info(">> animal2 " + animal2 + " id=" + JDOHelper.getObjectId(animal2) + " farm=" + JDOHelper.getObjectId(animal2.getFarm()));
        LOG.info(">> animal3 " + animal3 + " id=" + JDOHelper.getObjectId(animal3) + " farm=" + JDOHelper.getObjectId(animal3.getFarm()));
        LOG.info(">> animal4 " + animal4 + " id=" + JDOHelper.getObjectId(animal4) + " farm=" + JDOHelper.getObjectId(animal4.getFarm()));
        LOG.info(">> animal5 " + animal5 + " id=" + JDOHelper.getObjectId(animal5) + " farm=" + JDOHelper.getObjectId(animal5.getFarm()));
        farm1.setAnimals(createSet(animal2, animal3, animal5));
        LOG.info(">> flush.start for change");
        pm.flush();
        LOG.info(">> flush.complete");
        // will move animal3 from farm2 to farm1
        // will move animal5 from farm3 to farm2
        // should result in:
        // farm1 <-> {animal2, animal3, animal5}
        // farm2 <-> {animal4}
        // farm3 <-> {}
        // i.e. animal3 and animal5 moved from their previous owners to farm1
        assertEquals(createSet(animal2, animal3, animal5), farm1.getAnimals());
        assertEquals(farm1, animal2.getFarm());
        assertEquals(farm1, animal3.getFarm());
        assertEquals(farm1, animal5.getFarm());
        assertEquals(createSet(animal4), farm2.getAnimals());
        assertEquals(farm2, animal4.getFarm());
        assertEquals(Collections.EMPTY_SET, farm3.getAnimals());
        tx.commit();
    } finally {
        try {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            if (pm != null) {
                pm.close();
            }
        } finally {
            clean(Farm.class);
            clean(Animal.class);
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) Farm(org.jpox.samples.one_many.bidir.Farm) TransactionMode(org.datanucleus.tests.annotations.TransactionMode)

Example 15 with Animal

use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.

the class AttachDetachTest method testAttachOneManyBidirFromNewManySide.

/**
 * Test of persisting a new object that has a N-1 bidir relation with a detached object.
 */
public void testAttachOneManyBidirFromNewManySide() {
    try {
        Farm detachedFarm = null;
        PersistenceManager pm = newPM();
        ((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
        Transaction tx = pm.currentTransaction();
        try {
            // Persist the 1 side
            tx.begin();
            Farm farm = new Farm("Sunnybrook Farm");
            pm.makePersistent(farm);
            tx.commit();
            detachedFarm = farm;
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create the N side and link to the detached 1 side
        Animal animal = new Animal("Porky");
        animal.setFarm(detachedFarm);
        detachedFarm.addAnimal(animal);
        pm = newPM();
        ((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
        tx = pm.currentTransaction();
        try {
            // Persist from the N side
            tx.begin();
            pm.makePersistent(animal);
            tx.commit();
            // Check the detached results
            assertTrue("Animal should be detached but isnt", JDOHelper.isDetached(animal));
            Farm farm = animal.getFarm();
            assertNotNull("Animal should have its Farm field set but is null", farm);
            assertTrue("Animal.farm should be detached but isnt", JDOHelper.isDetached(farm));
            assertNotNull("Animal.farm.animals should be set but is null", farm.getAnimals());
            assertEquals("Animal.farm.animals has incorrect number of animals", 1, farm.getAnimals().size());
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) Farm(org.jpox.samples.one_many.bidir.Farm) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)18 Transaction (javax.jdo.Transaction)18 Animal (org.jpox.samples.one_many.bidir.Animal)18 Farm (org.jpox.samples.one_many.bidir.Farm)18 JDOUserException (javax.jdo.JDOUserException)7 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)6 DairyFarm (org.jpox.samples.one_many.bidir.DairyFarm)6 List (java.util.List)4 Query (javax.jdo.Query)4 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)4 Iterator (java.util.Iterator)3 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)3 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Extent (javax.jdo.Extent)1 StoreManager (org.datanucleus.store.StoreManager)1 TransactionMode (org.datanucleus.tests.annotations.TransactionMode)1 Manager (org.jpox.samples.models.company.Manager)1 Cattle (org.jpox.samples.one_many.bidir.Cattle)1