use of org.datanucleus.samples.annotations.one_many.bidir.Farm in project tests by datanucleus.
the class MultithreadedTest method processQueryAndDetachOneManyFKBidir.
protected String processQueryAndDetachOneManyFKBidir(boolean transaction) {
List<Animal> results = null;
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
if (transaction) {
tx.begin();
}
Query q = em.createQuery("SELECT a FROM " + Animal.class.getName() + " a");
List<Animal> animals = q.getResultList();
for (Animal a : animals) {
Farm f = a.getFarm();
f.getAnimals();
}
results = new ArrayList<Animal>(animals);
if (transaction) {
tx.commit();
}
} catch (Throwable thr) {
LOG.error("Exception query objects", thr);
return "Exception in query : " + thr.getMessage();
} finally {
if (transaction && tx.isActive()) {
tx.rollback();
}
// Detached the Animals and their loaded fields
em.close();
}
for (Animal a : results) {
try {
LOG.debug(">> Animal: " + a);
if (a.getFarm() == null) {
return "Animal has null Farm";
}
Farm f = a.getFarm();
if (f.getAnimals() == null || f.getAnimals().size() != 100) {
return "Animal has Farm with incorrect Animals";
}
} catch (Exception exc) {
LOG.error(">> Exception thrown on check of results", exc);
return "Exception checking results : " + exc.getMessage();
}
}
return null;
}
use of org.datanucleus.samples.annotations.one_many.bidir.Farm in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFromJoinOneToManyUsingAttributeName.
/**
* Test basic generation of query with candidate and alias, and FROM join on 1-N (using attr name).
*/
public void testBasicWithFromJoinOneToManyUsingAttributeName() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
Root<Farm> candidate = crit.from(Farm.class);
candidate.alias("f");
crit.select(candidate);
Join animalJoin = candidate.join("f.animals");
animalJoin.alias("a");
Path nameField = animalJoin.get("name");
Predicate nameEquals = cb.equal(nameField, "Woolly Sheep");
crit.where(nameEquals);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT f FROM org.datanucleus.samples.annotations.one_many.bidir.Farm f JOIN f.animals a WHERE (a.name = 'Woolly Sheep')", crit.toString());
Query q = em.createQuery(crit);
List<Farm> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 1, results.size());
Farm farm = (Farm) results.get(0);
assertEquals("Farm is incorrect", "Kiwi Farm", farm.getName());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.samples.annotations.one_many.bidir.Farm in project tests by datanucleus.
the class CriteriaStringsTest method setUp.
/* (non-Javadoc)
* @see org.datanucleus.tests.PersistenceTestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@datanucleus.org");
p1.setAge(35);
em.persist(p1);
Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@datanucleus.org");
p2.setAge(38);
p2.setBestFriend(p1);
em.persist(p2);
// 1-1 relation sample
Employee emp1 = new Employee(105, "Joe", "Bloggs", "joe.bloggs@datanucleus.org", 12000.0f, "10005");
emp1.setAge(40);
Employee emp2 = new Employee(106, "Nigel", "Bloggs", "nigel.bloggs@datanucleus.org", 13000.0f, "10006");
emp2.setAge(41);
Account acc1 = new Account();
acc1.setId(105);
acc1.setUsername("joebloggs");
acc1.setEnabled(true);
Account acc2 = new Account();
acc2.setId(106);
acc2.setUsername("nigelbloggs");
acc2.setEnabled(true);
emp1.setAccount(acc1);
emp2.setAccount(acc2);
Qualification q1 = new Qualification("BSc");
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.YEAR, 2001);
cal.set(Calendar.MONTH, 5);
cal.set(Calendar.DAY_OF_MONTH, 1);
q1.setDate(cal.getTime());
q1.setPerson(emp1);
Qualification q2 = new Qualification("MSc");
q2.setPerson(emp2);
cal.set(Calendar.YEAR, 2011);
cal.set(Calendar.MONTH, 4);
cal.set(Calendar.DAY_OF_MONTH, 1);
q2.setDate(cal.getTime());
em.persist(emp1);
em.persist(emp2);
em.persist(q1);
em.persist(q2);
// 1-N relation sample
Farm farm1 = new Farm("Giles Farm");
Farm farm2 = new Farm("Kiwi Farm");
Animal an1 = new Animal("Brown Cow");
Animal an2 = new Animal("Woolly Sheep");
Animal an3 = new Animal("Sheepdog");
farm1.getAnimals().add(an1);
an1.setFarm(farm1);
farm2.getAnimals().add(an2);
an2.setFarm(farm2);
farm2.getAnimals().add(an3);
an3.setFarm(farm2);
em.persist(farm1);
em.persist(farm2);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception in setUp data", e);
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations