Search in sources :

Example 1 with Manager

use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.

the class CriteriaMetaModelTest method testLeftOuterJoinOnQuery.

/**
 * Test generation of FROM with left outer join and ON clause.
 */
public void testLeftOuterJoinOnQuery() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        // "SELECT Object(T) FROM " + Team.class.getName() + " T " +
        // "JOIN T.manager M ON M.lastName = 'Mourinho'").getResultList();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Team> crit = cb.createQuery(Team.class);
        Root<Team> candidate = crit.from(Team.class);
        candidate.alias("t");
        crit.select(candidate);
        Join<Team, Manager> mgrRoot = candidate.join(Team_.manager, JoinType.INNER);
        mgrRoot.alias("m");
        Predicate onCond = cb.equal(mgrRoot.get(Manager_.lastName), "Mourinho");
        mgrRoot.on(onCond);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT t FROM org.datanucleus.samples.jpa.query.Team t JOIN t.manager m ON (m.lastName = 'Mourinho')", crit.toString());
        Query q = em.createQuery(crit);
        List<Team> teams = q.getResultList();
        assertNotNull("Null results returned!", teams);
        assertEquals("Number of results is incorrect", 1, teams.size());
        boolean realmadrid = false;
        Iterator<Team> teamIter = teams.iterator();
        while (teamIter.hasNext()) {
            Team team = teamIter.next();
            if (team.getName().equals("Real Madrid")) {
                realmadrid = true;
            }
        }
        assertTrue("Real Madrid not returned", realmadrid);
        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) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) Team(org.datanucleus.samples.jpa.query.Team)

Example 2 with Manager

use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.

the class CriteriaMetaModelTest method testFilter2.

/**
 * Test basic querying for a candidate with a filter with 2 clauses
 */
public void testFilter2() {
    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);
        candidate.alias("m");
        crit.select(candidate);
        Predicate yrsUpper = cb.lessThan(candidate.get(Manager_.yearsExperience), 5);
        Predicate yrsLower = cb.greaterThanOrEqualTo(candidate.get(Manager_.yearsExperience), 2);
        crit.where(yrsUpper, yrsLower);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (m.yearsExperience < 5) AND (m.yearsExperience >= 2)", crit.toString());
        Query q = em.createQuery(crit);
        List<Manager> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        Iterator<Manager> iter = results.iterator();
        boolean guardiola = false;
        while (iter.hasNext()) {
            Manager mgr = iter.next();
            if (mgr.getFirstName().equals("Pep") && mgr.getLastName().equals("Guardiola")) {
                guardiola = true;
            }
        }
        assertTrue("Guardiola was not returned!", guardiola);
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager) Predicate(javax.persistence.criteria.Predicate)

Example 3 with Manager

use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.

the class CriteriaMetaModelTest method testResultWithOrder.

/**
 * Test basic querying with a result with order.
 */
public void testResultWithOrder() {
    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);
        candidate.alias("m");
        crit.multiselect(candidate.get(Manager_.firstName), candidate.get(Manager_.lastName));
        crit.orderBy(cb.desc(candidate.get(Manager_.yearsExperience)));
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m.firstName,m.lastName FROM org.datanucleus.samples.jpa.query.Manager m ORDER BY m.yearsExperience DESC", 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) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager)

Example 4 with Manager

use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.

the class CriteriaMetaModelTest method testFunction.

/**
 * Test criteria query with a function (SQL function).
 */
public void testFunction() {
    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);
        candidate.alias("m");
        crit.select(candidate);
        Expression<String> upperExpr = cb.function("UPPER", String.class, candidate.get(Manager_.firstName));
        Predicate firstNameUpperCaseEquals = cb.equal(upperExpr, "PEP");
        crit.where(firstNameUpperCaseEquals);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (FUNCTION('UPPER',m.firstName) = 'PEP')", crit.toString());
        Query q = em.createQuery(crit);
        List<Manager> managers = q.getResultList();
        assertNotNull("Managers is null!", managers);
        assertEquals("Number of managers is wrong", 1, managers.size());
        Manager mgr1 = managers.get(0);
        assertEquals("Pep", mgr1.getFirstName());
        assertEquals("Guardiola", mgr1.getLastName());
        tx.rollback();
    } catch (Exception e) {
        LOG.error("Exception during query", e);
        fail("Exception during test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager) Predicate(javax.persistence.criteria.Predicate)

Example 5 with Manager

use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.

the class CriteriaMetaModelTest method testFilterIn.

/**
 * Test basic querying using IN clause.
 */
public void testFilterIn() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        // Test 1 : IN
        CriteriaQuery<Manager> crit = cb.createQuery(Manager.class);
        Root<Manager> candidate = crit.from(Manager.class);
        candidate.alias("m");
        crit.select(candidate);
        Predicate pred = candidate.get(Manager_.firstName).in("Jose", "Pep");
        crit.where(pred);
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE m.firstName IN ('Jose','Pep')", crit.toString());
        Query q = em.createQuery(crit);
        List<Manager> managers = q.getResultList();
        assertNotNull("Managers is null!", managers);
        assertEquals("Number of managers is wrong", 2, managers.size());
        // Test 2 : NOT IN
        CriteriaQuery<Manager> crit2 = cb.createQuery(Manager.class);
        Root<Manager> candidate2 = crit2.from(Manager.class);
        candidate2.alias("m");
        crit2.select(candidate2);
        Predicate pred2 = candidate2.get(Manager_.firstName).in("Jose", "Pep").not();
        crit2.where(pred2);
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE !(m.firstName IN ('Jose','Pep'))", crit2.toString());
        Query q2 = em.createQuery(crit2);
        List<Manager> managers2 = q2.getResultList();
        assertNotNull("Managers is null!", managers2);
        assertEquals("Number of managers is wrong", 0, managers2.size());
        // Test 3 : IN {collParameter}
        CriteriaQuery<Manager> crit3 = cb.createQuery(Manager.class);
        Root<Manager> candidate3 = crit3.from(Manager.class);
        candidate3.alias("m");
        crit3.select(candidate3);
        ParameterExpression paramExpr = cb.parameter(List.class, "inParam");
        Predicate pred3 = candidate3.get(Manager_.firstName).in(paramExpr);
        crit3.where(pred3);
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE m.firstName IN (:inParam)", crit3.toString());
        Query q3 = em.createQuery(crit3);
        List<String> inParamColl = new ArrayList();
        inParamColl.add("Jose");
        inParamColl.add("Pep");
        q3.setParameter("inParam", inParamColl);
        List<Manager> managers3 = q3.getResultList();
        assertNotNull("Managers is null!", managers3);
        assertEquals("Number of managers is wrong", 2, managers3.size());
        // Test 3 : IN {stringArrayParameter}
        CriteriaQuery<Manager> crit4 = cb.createQuery(Manager.class);
        Root<Manager> candidate4 = crit4.from(Manager.class);
        candidate4.alias("m");
        crit4.select(candidate4);
        ParameterExpression paramExpr4 = cb.parameter(String[].class, "inParam");
        Predicate pred4 = candidate4.get(Manager_.firstName).in(paramExpr4);
        crit4.where(pred4);
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE m.firstName IN (:inParam)", crit4.toString());
        Query q4 = em.createQuery(crit4);
        q4.setParameter("inParam", new String[] { "Jose", "Pep" });
        List<Manager> managers4 = q4.getResultList();
        assertNotNull("Managers is null!", managers4);
        assertEquals("Number of managers is wrong", 2, managers4.size());
        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) ArrayList(java.util.ArrayList) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) ParameterExpression(javax.persistence.criteria.ParameterExpression)

Aggregations

EntityManager (javax.persistence.EntityManager)16 EntityTransaction (javax.persistence.EntityTransaction)16 Manager (org.datanucleus.samples.jpa.query.Manager)16 Query (javax.persistence.Query)15 TypedQuery (javax.persistence.TypedQuery)15 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)15 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)14 Predicate (javax.persistence.criteria.Predicate)10 Team (org.datanucleus.samples.jpa.query.Team)4 Fetch (javax.persistence.criteria.Fetch)3 Join (javax.persistence.criteria.Join)3 Player (org.datanucleus.samples.jpa.query.Player)3 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 Tuple (javax.persistence.Tuple)1 TupleElement (javax.persistence.TupleElement)1 ParameterExpression (javax.persistence.criteria.ParameterExpression)1