use of org.datanucleus.samples.annotations.models.company.Organisation in project tests by datanucleus.
the class JPQLSubqueryTest method testHavingSubquery.
/**
* Simple query using a subquery in the HAVING clause.
*/
public void testHavingSubquery() {
if (!(storeMgr instanceof RDBMSStoreManager)) {
return;
}
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.SUBQUERY_IN_HAVING)) {
return;
}
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Account a1 = new Account();
a1.setUsername("Flintstone");
a1.setId(1);
em.persist(a1);
Organisation o1 = new Organisation("Flintstone");
o1.setDescription("Freds organisation");
em.persist(o1);
// TODO Come up with a better sample query. Why does the JPA TCK/Spec have none?
List<Person> result = em.createQuery("SELECT o FROM " + Organisation.class.getName() + " o " + "GROUP BY o.name HAVING EXISTS (SELECT a FROM " + Account.class.getName() + " a WHERE a.username = o.name)").getResultList();
assertNotNull(result);
assertEquals(1, result.size());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception in query", e);
fail("Exception executing query with HAVING subquery : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Person.class);
}
}
Aggregations