Search in sources :

Example 6 with Manager

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

the class CriteriaMetaModelTest method testFilter.

/**
 * Test basic querying for a candidate with a filter
 */
public void testFilter() {
    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 ageLessThan = cb.lessThan(candidate.get(Manager_.yearsExperience), 5);
        crit.where(ageLessThan);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (m.yearsExperience < 5)", 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 7 with Manager

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

the class CriteriaMetaModelTest method testFilterWithNullOrdering.

/**
 * Test basic querying specifying null order clause
 */
public void testFilterWithNullOrdering() {
    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 ageLessThan = cb.greaterThan(candidate.get(Manager_.yearsExperience), 1);
        crit.where(ageLessThan);
        crit.orderBy(Collections.EMPTY_LIST);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (m.yearsExperience > 1)", 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());
        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 8 with Manager

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

the class CriteriaMetaModelTest method tearDown.

/* (non-Javadoc)
     * @see org.datanucleus.tests.PersistenceTestCase#tearDown()
     */
@Override
protected void tearDown() throws Exception {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        Query q = em.createQuery("SELECT t FROM Team t");
        List<Team> teams = q.getResultList();
        Iterator<Team> teamIter = teams.iterator();
        while (teamIter.hasNext()) {
            Team team = teamIter.next();
            Manager mgr = team.getManager();
            mgr.setTeam(null);
            team.setManager(null);
        }
        Query q2 = em.createQuery("SELECT p FROM Player p");
        List<Player> players = q2.getResultList();
        Iterator<Player> playerIter = players.iterator();
        while (playerIter.hasNext()) {
            Player pl = playerIter.next();
            Team t = pl.getTeam();
            t.getPlayers().remove(pl);
            pl.setTeam(null);
        }
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception in tearDown", e);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    clean(Player.class);
    clean(Team.class);
    clean(Manager.class);
    clean(Person.class);
    super.tearDown();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Player(org.datanucleus.samples.jpa.query.Player) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Team(org.datanucleus.samples.jpa.query.Team) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager)

Example 9 with Manager

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

the class CriteriaMetaModelTest method testFilterWithOrder.

/**
 * Test basic querying for a candidate with an order.
 */
public void testFilterWithOrder() {
    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 ageLessThan = cb.greaterThan(candidate.get(Manager_.yearsExperience), 1);
        crit.where(ageLessThan);
        crit.orderBy(cb.asc(candidate.get(Manager_.yearsExperience)));
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (m.yearsExperience > 1) ORDER BY m.yearsExperience ASC", 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());
        Manager mgr1 = managers.get(0);
        Manager mgr2 = managers.get(1);
        assertEquals("Pep", mgr1.getFirstName());
        assertEquals("Guardiola", mgr1.getLastName());
        assertEquals(3, mgr1.getYearsExperience());
        assertEquals("Jose", mgr2.getFirstName());
        assertEquals("Mourinho", mgr2.getLastName());
        assertEquals(8, mgr2.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 10 with Manager

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

the class CriteriaMetaModelTest method setUp.

/* (non-Javadoc)
     * @see org.datanucleus.tests.PersistenceTestCase#setUp()
     */
@Override
protected void setUp() throws Exception {
    super.setUp();
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        Team team1 = new Team(1, "Real Madrid");
        Manager mgr1 = new Manager(1, "Jose", "Mourinho", 8);
        mgr1.setTeam(team1);
        team1.setManager(mgr1);
        em.persist(team1);
        Team team2 = new Team(2, "Barcelona");
        Manager mgr2 = new Manager(2, "Pep", "Guardiola", 3);
        mgr2.setTeam(team2);
        team2.setManager(mgr2);
        em.persist(team2);
        Player pl1 = new Player(101, "David", "Beckham");
        Calendar cal = GregorianCalendar.getInstance();
        cal.set(Calendar.YEAR, 1989);
        cal.set(Calendar.MONTH, 8);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        pl1.setStartDate(cal.getTime());
        pl1.setTeam(team1);
        pl1.setDateTime(LocalDateTime.now());
        em.persist(pl1);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Player(org.datanucleus.samples.jpa.query.Player) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Team(org.datanucleus.samples.jpa.query.Team) EntityManager(javax.persistence.EntityManager) Manager(org.datanucleus.samples.jpa.query.Manager)

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