use of org.datanucleus.samples.annotations.models.company.Qualification in project tests by datanucleus.
the class JPQLQueryTest method testWhereTREAT.
/**
* Test for TREAT in WHERE clause.
*/
public void testWhereTREAT() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
bart.addSubordinate(boss);
bart.addSubordinate(boss2);
Qualification q1 = new Qualification("q1");
q1.setPerson(boss);
Qualification q2 = new Qualification("q2");
q2.setPerson(boss2);
em.persist(bart);
em.persist(homer);
em.persist(boss);
em.persist(boss2);
em.persist(q1);
em.persist(q2);
em.flush();
List<Qualification> results = em.createQuery("SELECT q FROM " + Qualification.class.getName() + " q WHERE (TREAT(person AS Employee_Ann)).serialNo = \"serial 3\"").getResultList();
assertEquals(1, results.size());
assertEquals("q1", ((Qualification) results.iterator().next()).getName());
tx.rollback();
} catch (PersistenceException e) {
LOG.error("Exception in test", e);
fail("Exception in TREAT WHERE test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
removeManagerEmployeeRelation();
clean(Qualification.class);
clean(Manager.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Qualification in project tests by datanucleus.
the class JPQLQueryTest method testJoinOnWithSubquery.
/**
* Test use of JOIN ON where the ON clause contains a subquery.
*/
public void testJoinOnWithSubquery() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@dn.org");
p1.setAge(31);
em.persist(p1);
Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@dn.org");
p2.setAge(35);
em.persist(p2);
Qualification q1 = new Qualification("BSc in Maths");
q1.setPerson(p1);
q1.setDate(new Date());
em.flush();
// Just check that this is queryable
Query q = em.createQuery("SELECT q.name FROM " + Qualification.class.getName() + " q JOIN q.person p ON p.age < (SELECT AVG(p2.age) FROM " + Person.class.getName() + " p2)");
q.getResultList();
// TODO Add some asserts, or choose a good example for this
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Qualification.class);
clean(Person.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Qualification in project tests by datanucleus.
the class JPQLQueryTest method testFromTREAT.
/**
* Test for TREAT in FROM clause.
*/
public void testFromTREAT() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
bart.addSubordinate(boss);
bart.addSubordinate(boss2);
Qualification q1 = new Qualification("q1");
q1.setPerson(boss);
Qualification q2 = new Qualification("q2");
q2.setPerson(boss2);
em.persist(bart);
em.persist(homer);
em.persist(boss);
em.persist(boss2);
em.persist(q1);
em.persist(q2);
em.flush();
List<Qualification> results = em.createQuery("SELECT q FROM " + Qualification.class.getName() + " q JOIN TREAT(q.person AS " + Employee.class.getName() + ") e WHERE e.serialNo = \"serial 3\"").getResultList();
assertEquals(1, results.size());
assertEquals("q1", ((Qualification) results.iterator().next()).getName());
tx.rollback();
} catch (PersistenceException e) {
LOG.error("Exception in test", e);
fail("Exception in TREAT FROM test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
removeManagerEmployeeRelation();
clean(Qualification.class);
clean(Manager.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Qualification in project tests by datanucleus.
the class CriteriaStringsTest method testTREATInWhereClause.
/**
* Test generation of filter with TREAT.
*/
public void testTREATInWhereClause() {
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);
Path<Employee> empPath = cb.treat(candidate.get("person"), Employee.class);
Path agePath = empPath.get("age");
crit.where(cb.gt(agePath, 40));
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT q FROM org.datanucleus.samples.annotations.models.company.Qualification q WHERE (TREAT(q.person AS " + Employee.class.getName() + ").age > 40)", 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());
tx.rollback();
} 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 testLessThanDate.
/**
* Test less than Date.
*/
public void testLessThanDate() {
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, 2011);
cal.set(Calendar.MONTH, 2);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Path datePath = candidate.get("date");
crit.where(cb.lessThan(datePath, new java.sql.Date(cal.getTime().getTime())));
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT q FROM org.datanucleus.samples.annotations.models.company.Qualification q WHERE (q.date < {d '2011-03-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());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations