use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.
the class MultithreadedTest method persistFarmWithAnimals.
/**
* Convenience method to create a Farm with 100 Animals. Used by various tests in this file
* @return The id of the Farm object
*/
private Object persistFarmWithAnimals() {
LOG.debug(">> Persisting data");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
Object farmId = "Jones Farm";
try {
tx.begin();
Farm farm = new Farm("Jones Farm");
em.persist(farm);
for (int i = 0; i < 100; i++) {
Animal an = new Animal("Sheep" + i);
an.setFarm(farm);
farm.getAnimals().add(an);
em.persist(an);
}
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception persisting objects", thr);
fail("Exception persisting data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
LOG.debug(">> Persisted data");
// Verify the persistence
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
LOG.debug(">> Querying Animals");
Query q = em.createQuery("SELECT a FROM " + Animal.class.getName() + " a");
List<Animal> ans = q.getResultList();
for (Animal a : ans) {
LOG.debug(">> animal=" + a + " farm=" + a.getFarm());
}
LOG.debug(">> Queried Animals");
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception checking objects", thr);
fail("Exception checking data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
return farmId;
}
use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.
the class RelationshipsTest method testOneToManyFKList.
/**
* Test of 1-N FK relation with ordered list
*/
public void testOneToManyFKList() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Farm farm1 = new Farm("Giles Farm");
Animal an1 = new Animal("Spot the Dog");
farm1.getAnimals().add(an1);
an1.setFarm(farm1);
Animal an2 = new Animal("Rosa the Rhino");
farm1.getAnimals().add(an2);
an2.setFarm(farm1);
em.persist(farm1);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating Farm and Animals");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
List result = em.createQuery("SELECT Object(T) FROM " + Farm.class.getName() + " T").getResultList();
assertEquals(1, result.size());
Farm farm = (Farm) result.get(0);
List<Animal> animals = farm.getAnimals();
assertEquals("Number of animals in Farm is incorrect", 2, animals.size());
Animal an1 = animals.get(0);
Animal an2 = animals.get(1);
assertEquals("First Animal in list has incorrect name", "Rosa the Rhino", an1.getName());
assertEquals("Second Animal in list has incorrect name", "Spot the Dog", an2.getName());
tx.rollback();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Farm and Animals");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.
the class JPQLQueryTest method testContainerSIZE.
/**
* Test for SIZE function of container field.
*/
public void testContainerSIZE() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Farm farm1 = new Farm("High Farm");
Farm farm2 = new Farm("Low Farm");
Animal a1 = new Animal("Dog");
Animal a2 = new Animal("Sheep");
Animal a3 = new Animal("Cow");
farm1.getAnimals().add(a1);
farm1.getAnimals().add(a2);
farm2.getAnimals().add(a3);
em.persist(farm1);
em.persist(farm2);
em.flush();
List result = em.createQuery("SELECT F FROM " + Farm.class.getName() + " F WHERE SIZE(animals) > 1").getResultList();
assertEquals(1, result.size());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.
the class JPQLQueryTest method testMemberOfViaUnboundVariableForeignKey.
/**
* Test of JPQL "MEMBER [OF] (container-expr)" syntax, using FK.
*/
public void testMemberOfViaUnboundVariableForeignKey() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Farm farm1 = new Farm("High Farm");
Farm farm2 = new Farm("Low Farm");
Animal a1 = new Animal("Dog");
Animal a2 = new Animal("Sheep");
Animal a3 = new Animal("Cow");
farm1.getAnimals().add(a1);
farm1.getAnimals().add(a2);
farm2.getAnimals().add(a3);
em.persist(farm1);
em.persist(farm2);
em.flush();
List result = em.createQuery("SELECT DISTINCT f FROM " + Farm.class.getName() + " f," + Animal.class.getName() + " a WHERE a MEMBER OF f.animals AND a.name = 'Dog'").getResultList();
// "High Farm"
assertEquals(1, result.size());
Farm farm = (Farm) result.get(0);
assertEquals("Farm returned from MEMBER OF query has incorrect name", "High Farm", farm.getName());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown generating query with MEMBER syntax " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
use of org.datanucleus.samples.annotations.one_many.bidir.Animal in project tests by datanucleus.
the class JPQLQueryTest method testInnerJoinOneToManyBiFKOwner.
/**
* Test for Inner Join 1-N bidirectional FK relation from the owner side.
*/
public void testInnerJoinOneToManyBiFKOwner() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Farm farm1 = new Farm("High Farm");
Farm farm2 = new Farm("Low Farm");
Animal a1 = new Animal("Dog");
Animal a2 = new Animal("Sheep");
Animal a3 = new Animal("Cow");
farm1.getAnimals().add(a1);
farm1.getAnimals().add(a2);
farm2.getAnimals().add(a3);
em.persist(farm1);
em.persist(farm2);
em.flush();
List result = em.createQuery("SELECT Object(F) FROM " + Farm.class.getName() + " F INNER JOIN F.animals A").getResultList();
assertEquals(3, result.size());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
Aggregations