use of org.jpox.samples.one_many.bidir.Animal 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);
}
}
use of org.jpox.samples.one_many.bidir.Animal 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);
}
}
use of org.jpox.samples.one_many.bidir.Animal 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);
}
}
use of org.jpox.samples.one_many.bidir.Animal 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);
}
}
use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.
the class ManagedRelationshipTest method testOneToManyFKBidirPersistElement.
/**
* Test for management of relations with a 1-N FK bidir where the objects are persisted
* and only the FK side is set.
*/
public void testOneToManyFKBidirPersistElement() {
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);
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", 0, farm.getAnimals().size());
pm.makePersistent(animal1);
pm.makePersistent(animal2);
pm.flush();
// TODO This will work if the collection is using lazy loading since it will not be loaded
// before here, and will load from the datastore when called.
// Check that the relation sides are both set
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 only FK side set", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
Aggregations