Search in sources :

Example 46 with CriteriaQuery

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

the class CriteriaStringsTest method testBasicWithoutAlias.

/**
 * Test basic generation of query with candidate and without the alias.
 */
public void testBasicWithoutAlias() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Person> crit = cb.createQuery(Person.class);
        Root<Person> candidate = crit.from(Person.class);
        crit.select(candidate);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT DN_THIS FROM org.datanucleus.samples.annotations.models.company.Person DN_THIS", crit.toString());
        Query q = em.createQuery(crit);
        List<Person> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 4, results.size());
        Iterator<Person> iter = results.iterator();
        boolean nigelBloggsPresent = false;
        boolean joeBloggsPresent = false;
        boolean fredFlintstonePresent = false;
        boolean barneyRubblePresent = false;
        while (iter.hasNext()) {
            Person pers = iter.next();
            if (pers.getFirstName().equals("Fred") && pers.getLastName().equals("Flintstone") && pers.getPersonNum() == 101) {
                fredFlintstonePresent = true;
            }
            if (pers.getFirstName().equals("Barney") && pers.getLastName().equals("Rubble") && pers.getPersonNum() == 102) {
                barneyRubblePresent = true;
            }
            if (pers.getFirstName().equals("Joe") && pers.getLastName().equals("Bloggs") && pers.getPersonNum() == 105) {
                joeBloggsPresent = true;
            }
            if (pers.getFirstName().equals("Nigel") && pers.getLastName().equals("Bloggs") && pers.getPersonNum() == 106) {
                nigelBloggsPresent = true;
            }
        }
        assertTrue("Fred Flintstone was not returned!", fredFlintstonePresent);
        assertTrue("Barney Rubble was not returned!", barneyRubblePresent);
        assertTrue("Joe Bloggs was not returned!", joeBloggsPresent);
        assertTrue("Fred Bloggs was not returned!", nigelBloggsPresent);
        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) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 47 with CriteriaQuery

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

the class CriteriaStringsTest method testBasicWithOrder.

/**
 * Test basic generation of query with candidate and alias, with ordering.
 */
public void testBasicWithOrder() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Person> crit = cb.createQuery(Person.class);
        Root<Person> candidate = crit.from(Person.class);
        candidate.alias("p");
        crit.select(candidate);
        Path firstNameField = candidate.get("firstName");
        Order orderFirstName = cb.desc(firstNameField);
        crit.orderBy(orderFirstName);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p ORDER BY p.firstName DESC", crit.toString());
        Query q = em.createQuery(crit);
        List<Person> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 4, results.size());
        Person per0 = results.get(0);
        Person per1 = results.get(1);
        Person per2 = results.get(2);
        Person per3 = results.get(3);
        if (!per0.getFirstName().equals("Nigel") || !per0.getLastName().equals("Bloggs") || per0.getPersonNum() != 106) {
            fail("Nigel Bloggs was not result 0");
        }
        if (!per1.getFirstName().equals("Joe") || !per1.getLastName().equals("Bloggs") || per1.getPersonNum() != 105) {
            fail("Joe Bloggs was not result 1");
        }
        if (!per2.getFirstName().equals("Fred") || !per2.getLastName().equals("Flintstone") || per2.getPersonNum() != 101) {
            fail("Fred Flintstone was not result 2");
        }
        if (!per3.getFirstName().equals("Barney") || !per3.getLastName().equals("Rubble") || per3.getPersonNum() != 102) {
            fail("Barney Rubble was not result 3");
        }
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) Order(javax.persistence.criteria.Order) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 48 with CriteriaQuery

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

the class CriteriaStringsTest method testBasicWithFilterStringUPPERLOWER.

/**
 * Test basic generation of query with candidate and alias and filter (UPPER/LOWER).
 */
public void testBasicWithFilterStringUPPERLOWER() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Person> crit = cb.createQuery(Person.class);
        Root<Person> candidate = crit.from(Person.class);
        candidate.alias("p");
        crit.select(candidate);
        Path firstNameField = candidate.get("firstName");
        Expression firstNameUpperExpr = cb.upper(firstNameField);
        Predicate firstNameUpperEquals = cb.equal(firstNameUpperExpr, "FRED");
        Path lastNameField = candidate.get("lastName");
        Expression lastNameLowerExpr = cb.lower(lastNameField);
        Predicate lastNameUpperEquals = cb.equal(lastNameLowerExpr, "flintstone");
        crit.where(firstNameUpperEquals, lastNameUpperEquals);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE" + " (UPPER(p.firstName) = 'FRED') AND (LOWER(p.lastName) = 'flintstone')", crit.toString());
        Query q = em.createQuery(crit);
        List<Person> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        Iterator<Person> iter = results.iterator();
        boolean fredPresent = false;
        while (iter.hasNext()) {
            Person pers = iter.next();
            if (pers.getFirstName().equals("Fred") && pers.getLastName().equals("Flintstone") && pers.getPersonNum() == 101) {
                fredPresent = true;
            }
        }
        assertTrue("Fred was not returned!", fredPresent);
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityTransaction(javax.persistence.EntityTransaction) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) Expression(javax.persistence.criteria.Expression) ParameterExpression(javax.persistence.criteria.ParameterExpression) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 49 with CriteriaQuery

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

the class CriteriaStringsTest method testProjectionOnManyToOne.

/**
 * Test projection of many-to-one field.
 */
public void testProjectionOnManyToOne() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
        Root<Animal> candidate = crit.from(Animal.class);
        candidate.alias("a");
        Path<Farm> farmPath = candidate.get("farm");
        crit.select(farmPath);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT a.farm FROM org.datanucleus.samples.annotations.one_many.bidir.Animal a", crit.toString());
        Query q = em.createQuery(crit);
        List<Farm> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 3, results.size());
        Object result = results.get(0);
        assertNotNull(result);
        assertTrue("Result is of incorrect type", result instanceof Farm);
        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) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm)

Example 50 with CriteriaQuery

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

the class CriteriaStringsTest method testSubqueryExists.

/**
 * Test use of an EXISTS subquery.
 */
public void testSubqueryExists() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Person> crit = cb.createQuery(Person.class);
        Root<Person> candidate = crit.from(Person.class);
        candidate.alias("p");
        crit.select(candidate);
        Subquery<Person> subCrit = crit.subquery(Person.class);
        Root<Person> subCandidate = subCrit.from(Person.class);
        subCandidate.alias("p2");
        subCrit.select(subCandidate);
        Path bestFriendField = subCandidate.get("bestFriend");
        Predicate bestFriendEqual = cb.equal(bestFriendField, candidate);
        subCrit.where(bestFriendEqual);
        Predicate existsBestFriend = cb.exists(subCrit);
        crit.where(existsBestFriend);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p2 FROM org.datanucleus.samples.annotations.models.company.Person p2 WHERE (p2.bestFriend = p)", subCrit.toString());
        // Cannot check on precise query since the subquery name will be a random number
        assertTrue("Main query containing subquery is incorrect", crit.toString().startsWith("SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE EXISTS "));
        Query q = em.createQuery(crit);
        List<Employee> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 1, results.size());
        Person p = (Person) results.get(0);
        assertEquals("Employee first name is incorrect", "Fred", p.getFirstName());
        assertEquals("Employee last name is incorrect", "Flintstone", p.getLastName());
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) Person(org.datanucleus.samples.annotations.models.company.Person) Predicate(javax.persistence.criteria.Predicate)

Aggregations

CriteriaQuery (javax.persistence.criteria.CriteriaQuery)155 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)112 EntityManager (javax.persistence.EntityManager)109 Query (javax.persistence.Query)109 TypedQuery (javax.persistence.TypedQuery)69 Predicate (javax.persistence.criteria.Predicate)51 EntityTransaction (javax.persistence.EntityTransaction)45 Root (javax.persistence.criteria.Root)40 List (java.util.List)22 Path (javax.persistence.criteria.Path)18 Test (org.testng.annotations.Test)16 UserTransaction (javax.transaction.UserTransaction)15 Person (org.datanucleus.samples.annotations.models.company.Person)15 Manager (org.datanucleus.samples.jpa.query.Manager)14 Specification (org.springframework.data.jpa.domain.Specification)14 Item (org.jpwh.model.querying.Item)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 Join (javax.persistence.criteria.Join)10 Session (org.hibernate.Session)9