Search in sources :

Example 6 with Farm

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

the class PersistenceManagerTest method testMakeTransientOwnerAndElements.

/**
 * Test of makeTransient() for owner and makeTransientAll() for the  owner elements.
 */
public void testMakeTransientOwnerAndElements() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // JoinTable 1-N relationship
            // Create a Manager with subordinate number 0
            createNewManager(pm, 0);
            // Make the Manager transient with all subordinates
            tx.begin();
            Manager m = (Manager) pm.getExtent(Manager.class, true).iterator().next();
            pm.retrieveAll(m.getSubordinates());
            pm.retrieveAll(m.getDepartments());
            pm.makeTransient(m);
            pm.makeTransientAll(m.getSubordinates());
            pm.makeTransientAll(m.getDepartments());
            tx.commit();
            // Check the result
            assertNull(JDOHelper.getObjectId(m));
            // Compare the managers
            Manager m1 = queryManager(0, pm);
            tx.begin();
            Assert.assertEquals(m.getBestFriend(), m1.getBestFriend());
            Assert.assertEquals(m.getLastName(), m1.getLastName());
            Assert.assertEquals(m.getFirstName(), m1.getFirstName());
            Assert.assertEquals(m.getEmailAddress(), m1.getEmailAddress());
            Assert.assertEquals(m.getPersonNum(), m1.getPersonNum());
            Assert.assertTrue("subordinates are not the same", Manager.compareSet(m.getSubordinates(), m1.getSubordinates()));
            Assert.assertTrue("departments are not the same", Manager.compareSet(m.getDepartments(), m1.getDepartments()));
            tx.commit();
            // FK 1-N relationship
            // Create owner with 2 elements.
            tx = pm.currentTransaction();
            tx.begin();
            Farm farm = new Farm("Giles Farm");
            Animal an1 = new Animal("Dog");
            an1.setFarm(farm);
            Animal an2 = new Animal("Cow");
            an2.setFarm(farm);
            farm.addAnimal(an1);
            farm.addAnimal(an2);
            pm.makePersistent(farm);
            tx.commit();
            Object id = pm.getObjectId(farm);
            // Make the owner and its elements transient
            tx = pm.currentTransaction();
            tx.begin();
            pm.getFetchPlan().addGroup(FetchPlan.ALL);
            Farm transientFarm = (Farm) pm.getObjectById(id, true);
            pm.retrieveAll(transientFarm.getAnimals());
            pm.makeTransient(transientFarm);
            pm.makeTransientAll(transientFarm.getAnimals());
            tx.commit();
            // Check the result
            Set transientAnimals = transientFarm.getAnimals();
            assertFalse("Owner object is not transient!", JDOHelper.isPersistent(transientFarm));
            assertEquals("Number of elements in transient owner is incorrect", transientAnimals.size(), 2);
            Iterator transientAnimalsIter = transientAnimals.iterator();
            boolean dogPresent = false;
            boolean cowPresent = false;
            while (transientAnimalsIter.hasNext()) {
                Animal transientAnimal = (Animal) transientAnimalsIter.next();
                assertFalse("Element object is not transient!", JDOHelper.isPersistent(transientAnimal));
                // Check that the Animal name field is the same as that persisted
                if (transientAnimal.getName().equals("Dog")) {
                    dogPresent = true;
                } else if (transientAnimal.getName().equals("Cow")) {
                    cowPresent = true;
                }
                // Check that the owner is the transient one
                assertEquals("Owner of transient animal is incorrect", StringUtils.toJVMIDString(transientAnimal.getFarm()), StringUtils.toJVMIDString(transientFarm));
            }
            assertTrue("First value animal is not present in the transient set", dogPresent);
            assertTrue("Second value animal is not present in the transient set", cowPresent);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
        clean(Manager.class);
        clean(Widget.class);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) 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) Iterator(java.util.Iterator) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 7 with Farm

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

the class ManagedRelationshipTest method testOneToManyFKBidirPersistElement2.

/**
 * Test for management of relations with a 1-N FK bidir where the objects are persisted
 * and only the FK side is set for one element but both set for other.
 */
public void testOneToManyFKBidirPersistElement2() {
    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");
            animal1.setFarm(farm);
            animal2.setFarm(farm);
            farm.addAnimal(animal2);
            assertNotNull("Animal1 has null Farm but should be not-null before persist", animal1.getFarm());
            assertNotNull("Animal2 has null Farm but should be not-null before persist", animal2.getFarm());
            assertEquals("Farm has Animals yet should be empty before persist", 1, farm.getAnimals().size());
            pm.makePersistent(animal1);
            pm.makePersistent(animal2);
            pm.flush();
            // Check that the relation sides are both set for all objects
            assertSame("Animal1 has incorrect Farm after persist/flush", farm, animal1.getFarm());
            assertSame("Animal2 has incorrect Farm after persist/flush", farm, 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 FK side set for one element", 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 8 with Farm

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

the class ManagedRelationshipTest method testOneToManyFKBidirPersistInconsistent.

/**
 * Test for management of relations with a 1-N FK bidir where the objects are persisted
 * and the owner side of elements is inconsistent with the container.
 */
public void testOneToManyFKBidirPersistInconsistent() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // farm1 -> animal1, animal2 BUT animal1 -> farm2, animal2 -> farm2
            Farm farm1 = new Farm("Giles Farm");
            Farm farm2 = new Farm("Warburton Farm");
            Animal animal1 = new Animal("Cow");
            Animal animal2 = new Animal("Dog");
            farm1.addAnimal(animal1);
            farm1.addAnimal(animal2);
            animal1.setFarm(farm2);
            animal2.setFarm(farm2);
            pm.makePersistent(farm1);
            pm.flush();
            fail("Should have thrown exception when persisting inconsistent 1-N FK bidir relation");
            tx.commit();
        } catch (Exception e) {
            // Success
            return;
        } 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 9 with Farm

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

the class JDOQLContainerTest method testContainsFieldOfCandidate.

/**
 * Tests contains() of the value of a field in the candidate.
 */
public void testContainsFieldOfCandidate() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some data
        tx.begin();
        Farm farm1 = new Farm("Jones Farm");
        Animal animal1 = new Animal("Dog");
        Animal animal2 = new Animal("Cow 1");
        Animal animal3 = new Animal("Cow 2");
        farm1.getAnimals().add(animal2);
        farm1.getAnimals().add(animal3);
        farm1.getAnimals().add(animal1);
        animal1.setFarm(farm1);
        animal2.setFarm(farm1);
        animal3.setFarm(farm1);
        farm1.setPet(animal1);
        Farm farm2 = new Farm("Smith Farm");
        Animal animal4 = new Animal("Pig 1");
        Animal animal5 = new Animal("Pig 2");
        farm2.getAnimals().add(animal4);
        farm2.getAnimals().add(animal5);
        animal4.setFarm(farm2);
        animal5.setFarm(farm2);
        pm.makePersistent(farm1);
        pm.makePersistent(farm2);
        pm.flush();
        // Query the data
        Query q = pm.newQuery("SELECT FROM " + Farm.class.getName() + " WHERE animals.contains(pet)");
        List results = (List) q.execute();
        assertEquals(1, results.size());
        assertEquals("Jones Farm", ((Farm) results.get(0)).getName());
        tx.rollback();
    } catch (Exception e) {
        LOG.error("Exception thrown during test", e);
        fail("Exception thrown while performing test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) DairyFarm(org.jpox.samples.one_many.bidir.DairyFarm) Farm(org.jpox.samples.one_many.bidir.Farm) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Example 10 with Farm

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

the class JDOQLContainerTest method testBulkFetchUsingFKExplicitParams.

/**
 * Tests use of bulk-fetch on collection via FK using explicit params.
 */
public void testBulkFetchUsingFKExplicitParams() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some data
        tx.begin();
        Farm farm1 = new Farm("Jones Farm");
        Animal animal1 = new Animal("Dog");
        Animal animal2 = new Animal("Cow 1");
        Animal animal3 = new Animal("Cow 2");
        farm1.getAnimals().add(animal2);
        farm1.getAnimals().add(animal3);
        farm1.getAnimals().add(animal1);
        animal1.setFarm(farm1);
        animal2.setFarm(farm1);
        animal3.setFarm(farm1);
        farm1.setPet(animal1);
        Farm farm2 = new Farm("Smith Farm");
        Animal animal4 = new Animal("Pig 1");
        Animal animal5 = new Animal("Pig 2");
        farm2.getAnimals().add(animal4);
        farm2.getAnimals().add(animal5);
        animal4.setFarm(farm2);
        animal5.setFarm(farm2);
        pm.makePersistent(farm1);
        pm.makePersistent(farm2);
        pm.flush();
        // Query the data with bulk-fetch enabled
        // Note : this doesn't actually check how many SQL were issued just that it passes
        Query q = pm.newQuery("SELECT FROM " + Farm.class.getName() + " WHERE name == farmName PARAMETERS java.lang.String farmName");
        q.addExtension("datanucleus.rdbms.query.multivaluedFetch", "exists");
        q.getFetchPlan().setGroup("all");
        List results = (List) q.execute("Jones Farm");
        assertEquals(1, results.size());
        assertEquals("Jones Farm", ((Farm) results.get(0)).getName());
        tx.rollback();
    } catch (Exception e) {
        LOG.error("Exception thrown during test", e);
        fail("Exception thrown while performing test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Animal(org.jpox.samples.one_many.bidir.Animal) DairyFarm(org.jpox.samples.one_many.bidir.DairyFarm) Farm(org.jpox.samples.one_many.bidir.Farm) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)19 Transaction (javax.jdo.Transaction)19 Farm (org.jpox.samples.one_many.bidir.Farm)19 Animal (org.jpox.samples.one_many.bidir.Animal)18 JDOUserException (javax.jdo.JDOUserException)8 DairyFarm (org.jpox.samples.one_many.bidir.DairyFarm)7 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)6 Query (javax.jdo.Query)5 List (java.util.List)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 JDOException (javax.jdo.JDOException)1 StoreManager (org.datanucleus.store.StoreManager)1 TransactionMode (org.datanucleus.tests.annotations.TransactionMode)1 Manager (org.jpox.samples.models.company.Manager)1