Search in sources :

Example 11 with Manager

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

the class CriteriaMetaModelTest method testResultCase.

/**
 * Test basic querying with a result including CASE expression.
 */
public void testResultCase() {
    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");
        Path<Integer> yrsVar = candidate.get(Manager_.yearsExperience);
        Predicate lessThan2 = cb.lessThan(yrsVar, 5);
        crit.multiselect(candidate.get(Manager_.firstName), candidate.get(Manager_.lastName), cb.selectCase().when(lessThan2, "Junior").otherwise("Senior"));
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m.firstName,m.lastName,CASE WHEN (m.yearsExperience < 5) THEN 'Junior' ELSE 'Senior' END 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(3, result.length);
            if (result[0].equals("Jose") && result[1].equals("Mourinho")) {
                mourinho = true;
                assertEquals("Senior", result[2]);
            } else if (result[0].equals("Pep") && result[1].equals("Guardiola")) {
                guardiola = true;
                assertEquals("Junior", result[2]);
            }
        }
        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) Predicate(javax.persistence.criteria.Predicate) Fetch(javax.persistence.criteria.Fetch) EntityManager(javax.persistence.EntityManager)

Example 12 with Manager

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

the class CriteriaMetaModelTest method testResultTuple.

/**
 * Test basic querying with a result as a Tuple.
 */
public void testResultTuple() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Tuple> crit = cb.createTupleQuery();
        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<Tuple> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 2, results.size());
        boolean mourinho = false;
        boolean guardiola = false;
        Iterator<Tuple> resultIter = results.iterator();
        while (resultIter.hasNext()) {
            Tuple result = resultIter.next();
            List<TupleElement<?>> tupleElements = result.getElements();
            assertEquals(2, tupleElements.size());
            Object elem0 = result.get(0);
            Object elem1 = result.get(1);
            assertTrue(elem0 instanceof String);
            assertTrue(elem1 instanceof String);
            String first = (String) elem0;
            String last = (String) elem1;
            if (first.equals("Jose") && last.equals("Mourinho")) {
                mourinho = true;
            } else if (first.equals("Pep") && last.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) TupleElement(javax.persistence.TupleElement) Tuple(javax.persistence.Tuple)

Example 13 with Manager

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

the class CriteriaMetaModelTest method testMultipleJoin.

/**
 * Test generation of FROM with multiple joins.
 */
public void testMultipleJoin() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Player> crit = cb.createQuery(Player.class);
        Root<Player> candidate = crit.from(Player.class);
        candidate.alias("p");
        crit.select(candidate);
        Join<Player, Team> teamRoot = candidate.join(Player_.team, JoinType.INNER);
        teamRoot.alias("t");
        Join<Team, Manager> mgrRoot = teamRoot.join(Team_.manager, JoinType.INNER);
        mgrRoot.alias("m");
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.jpa.query.Player p JOIN p.team t JOIN t.manager m", crit.toString());
        Query q = em.createQuery(crit);
        List<Player> players = q.getResultList();
        assertNotNull("Null results returned!", players);
        assertEquals("Number of results is incorrect", 1, players.size());
        boolean beckham = false;
        Iterator<Player> playerIter = players.iterator();
        while (playerIter.hasNext()) {
            Player pl = playerIter.next();
            if (pl.getFirstName().equals("David")) {
                beckham = true;
            }
        }
        assertTrue("Beckham not returned", beckham);
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) Player(org.datanucleus.samples.jpa.query.Player) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager) EntityManager(javax.persistence.EntityManager) Team(org.datanucleus.samples.jpa.query.Team)

Example 14 with Manager

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

the class CriteriaMetaModelTest method testFilterWithMethodAndNamedParameter.

/**
 * Test basic querying for a candidate with a filter.
 */
public void testFilterWithMethodAndNamedParameter() {
    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);
        ParameterExpression<String> param = cb.parameter(String.class, "myParam");
        Predicate ageLessThan = cb.like(candidate.get(Manager_.firstName), param);
        crit.where(ageLessThan);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE m.firstName LIKE :myParam", crit.toString());
        Query q = em.createQuery(crit);
        q.setParameter("myParam", "Jo%");
        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("Jose", mgr1.getFirstName());
        assertEquals("Mourinho", mgr1.getLastName());
        assertEquals(8, mgr1.getYearsExperience());
        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 15 with Manager

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

the class CriteriaMetaModelTest method testFilterWithMethodAndUnnamedParameter.

/**
 * Test basic querying for a candidate with a filter.
 */
public void testFilterWithMethodAndUnnamedParameter() {
    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);
        ParameterExpression<String> param = cb.parameter(String.class);
        Predicate ageLessThan = cb.like(candidate.get(Manager_.firstName), param);
        crit.where(ageLessThan);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE m.firstName LIKE :DN_PARAM_0", crit.toString());
        Query q = em.createQuery(crit);
        q.setParameter(param, "Jo%");
        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("Jose", mgr1.getFirstName());
        assertEquals("Mourinho", mgr1.getLastName());
        assertEquals(8, mgr1.getYearsExperience());
        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)

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