Search in sources :

Example 1 with Farm

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

the class RelationshipTest method test1toNBidirFK.

/**
 * Test case for 1-N bidir FK relations.
 */
public void test1toNBidirFK() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "" + manageRelationships);
        Transaction tx = pm.currentTransaction();
        Object farmId = null;
        // Check the persistence of owner and elements
        try {
            tx.begin();
            // Create some data
            Farm farm = new Farm("Giles Farm");
            Animal animal1 = new Animal("Shep");
            Animal animal2 = new Animal("Grunter");
            farm.addAnimal(animal1);
            farm.addAnimal(animal2);
            animal1.setFarm(farm);
            animal2.setFarm(farm);
            pm.makePersistent(farm);
            tx.commit();
            farmId = pm.getObjectId(farm);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while creating 1-N bidir FK relationships : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the retrieval of owner and elements
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Farm farm = (Farm) pm.getObjectById(farmId);
            assertNotNull("Unable to retrieve container object for 1-N bidir FK relationship", farm);
            Collection animals = farm.getAnimals();
            assertNotNull("Holder of elements with 1-N bidir FK relation is null!", farm);
            assertEquals("Number of elements in 1-N bidir FK relationship is wrong", 2, animals.size());
            Iterator iter = animals.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while querying 1-N bidir FK relationships : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm.close();
    } finally {
        // Clean out our data
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) 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) Iterator(java.util.Iterator) Collection(java.util.Collection) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 2 with Farm

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

the class AttachDetachTest method testDetachAttach_OneToManyBidir.

/**
 * Test for a 1-N bidirectional relationship which, by its nature,
 * provides a recursive detach test.
 */
public void testDetachAttach_OneToManyBidir() {
    try {
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        Object id = null;
        try {
            // Persist some objects
            tx.begin();
            Farm farm = new Farm("North End Farm");
            Animal duck = new Animal("Donald");
            Animal cow = new Animal("Gertrude");
            Animal horse = new Animal("Shergar");
            farm.addAnimal(duck);
            farm.addAnimal(cow);
            farm.addAnimal(horse);
            pm.makePersistent(farm);
            tx.commit();
            id = pm.getObjectId(farm);
        } catch (Exception e) {
            fail("Exception thrown while persisting 1-N bidirectional objects for the recursive attach/detach test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = newPM();
        tx = pm.currentTransaction();
        Farm detachedFarm = null;
        try {
            tx.begin();
            pm.getFetchPlan().addGroup(FetchPlan.ALL);
            pm.getFetchPlan().setMaxFetchDepth(2);
            Farm farm = (Farm) pm.getObjectById(id, false);
            assertTrue("Error retrieving the Farm object that was just persisted", farm != null);
            LOG.info("Retrieved Farm \"" + farm.toString() + "\"");
            detachedFarm = (Farm) pm.detachCopy(farm);
            tx.commit();
        } catch (Exception e) {
            fail("Exception thrown while detaching 1-N bidrectional objects in the recursive attach/detach test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Play with the farm
        try {
            LOG.info("Detached farm : " + detachedFarm);
            Set<Animal> animals = detachedFarm.getAnimals();
            Iterator<Animal> animalsIter = animals.iterator();
            while (animalsIter.hasNext()) {
                LOG.info("Detached animal : " + animalsIter.next());
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown on use of allegedly detached farm " + e.getMessage());
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : 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)

Example 3 with Farm

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

the class JDOQLContainerTest method testNotContainsFieldOfCandidate.

/**
 * Tests !contains() of the value of a field in the candidate.
 */
public void testNotContainsFieldOfCandidate() {
    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);
        Animal animal6 = new Animal("Cat");
        farm2.setPet(animal6);
        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("Smith 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 4 with Farm

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

the class JDOQLContainerTest method testBulkFetchUsingFK.

/**
 * Tests use of bulk-fetch on collection via FK.
 */
public void testBulkFetchUsingFK() {
    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");
        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)

Example 5 with Farm

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

the class JDOQLResultTest method testCountOnClassHierarchy.

/**
 * Test of count(this) with inheritance using an Extent (inc subclasses).
 */
public void testCountOnClassHierarchy() {
    try {
        // Create objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Create a base object
            Farm baseobject = new Farm("base farm");
            pm.makePersistent(baseobject);
            // Create a sub object
            DairyFarm subobject = new DairyFarm("dairy farm");
            pm.makePersistent(subobject);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown during create of base and subclass objects for \"new-table\" strategy inheritance tree : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            Query q = pm.newQuery(pm.getExtent(Farm.class, true));
            q.setResult("count(this)");
            assertEquals(2, ((Long) q.execute()).longValue());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown during count(this) query" + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Farm.class);
        clean(DairyFarm.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) DairyFarm(org.jpox.samples.one_many.bidir.DairyFarm) Farm(org.jpox.samples.one_many.bidir.Farm) DairyFarm(org.jpox.samples.one_many.bidir.DairyFarm) JDOException(javax.jdo.JDOException) 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