Search in sources :

Example 6 with Fetch

use of javax.persistence.criteria.Fetch in project tests by datanucleus.

the class CriteriaMetaModelTest method testResult.

/**
 * Test basic querying with a result.
 */
public void testResult() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Manager> crit = cb.createQuery(Manager.class);
        Root<Manager> candidate = crit.from(Manager.class);
        Set<Join<Manager, ?>> joins = candidate.getJoins();
        // Make sure joins returns empty set
        assertNotNull(joins);
        assertEquals(0, joins.size());
        Set<Fetch<Manager, ?>> fetches = candidate.getFetches();
        // Make sure fetches returns empty set
        assertNotNull(fetches);
        assertEquals(0, fetches.size());
        candidate.alias("m");
        crit.multiselect(candidate.get(Manager_.firstName), candidate.get(Manager_.lastName));
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m.firstName,m.lastName FROM org.datanucleus.samples.jpa.query.Manager m", crit.toString());
        Query q = em.createQuery(crit);
        List<Object[]> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 2, results.size());
        boolean mourinho = false;
        boolean guardiola = false;
        Iterator<Object[]> resultIter = results.iterator();
        while (resultIter.hasNext()) {
            Object[] result = resultIter.next();
            assertEquals(2, result.length);
            if (result[0].equals("Jose") && result[1].equals("Mourinho")) {
                mourinho = true;
            } else if (result[0].equals("Pep") && result[1].equals("Guardiola")) {
                guardiola = true;
            }
        }
        if (!mourinho) {
            fail("Jose Mourinho not returned");
        }
        if (!guardiola) {
            fail("Pep Guardiola not returned");
        }
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Join(javax.persistence.criteria.Join) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager) Fetch(javax.persistence.criteria.Fetch) EntityManager(javax.persistence.EntityManager)

Aggregations

Fetch (javax.persistence.criteria.Fetch)6 EntityManager (javax.persistence.EntityManager)4 Query (javax.persistence.Query)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)4 Join (javax.persistence.criteria.Join)4 EntityTransaction (javax.persistence.EntityTransaction)3 TypedQuery (javax.persistence.TypedQuery)3 Manager (org.datanucleus.samples.jpa.query.Manager)3 WQConstraint (com.peterphi.std.guice.restclient.jaxb.webquery.WQConstraint)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1 Tuple (javax.persistence.Tuple)1 TupleElement (javax.persistence.TupleElement)1 Predicate (javax.persistence.criteria.Predicate)1 Root (javax.persistence.criteria.Root)1 UserTransaction (javax.transaction.UserTransaction)1