use of org.datanucleus.samples.annotations.models.company.Qualification 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();
}
}
use of org.datanucleus.samples.annotations.models.company.Qualification in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFilterLessThanDateLiteral.
/**
* Test basic generation of query with candidate and alias and filter (<).
*/
public void testBasicWithFilterLessThanDateLiteral() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Qualification> crit = cb.createQuery(Qualification.class);
Root<Qualification> candidate = crit.from(Qualification.class);
candidate.alias("q");
crit.select(candidate);
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date date = new Date(cal.getTime().getTime());
Path dateField = candidate.get("date");
Predicate dateLessThan = cb.lessThan(dateField, date);
crit.where(dateLessThan);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT q FROM org.datanucleus.samples.annotations.models.company.Qualification q WHERE (q.date < {d '2006-02-01'})", crit.toString());
Query q = em.createQuery(crit);
List<Qualification> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 1, results.size());
Qualification qual = results.iterator().next();
assertEquals("BSc", qual.getName());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations