Search in sources :

Example 51 with CriteriaQuery

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

the class CriteriaStringsTest method testBasicWithFromJoinOneToOne.

/**
 * Test basic generation of query with candidate and alias, and FROM join on 1-1.
 */
public void testBasicWithFromJoinOneToOne() {
    EntityManager em = getEM();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        CriteriaBuilder cb = emf.getCriteriaBuilder();
        CriteriaQuery<Employee> crit = cb.createQuery(Employee.class);
        Root<Employee> candidate = crit.from(Employee.class);
        candidate.alias("e");
        crit.select(candidate);
        Metamodel model = emf.getMetamodel();
        ManagedType empType = model.managedType(Employee.class);
        Attribute bAttr = empType.getAttribute("account");
        Join accountJoin = candidate.join((SingularAttribute) bAttr);
        accountJoin.alias("a");
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT e FROM org.datanucleus.samples.annotations.models.company.Employee e JOIN e.account a", crit.toString());
        Query q = em.createQuery(crit);
        List<Employee> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 2, results.size());
        Iterator<Employee> iter = results.iterator();
        boolean joeExists = false;
        boolean nigelExists = false;
        while (iter.hasNext()) {
            Employee emp = iter.next();
            if (emp.getFirstName().equals("Nigel") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 106) {
                nigelExists = true;
            } else if (emp.getFirstName().equals("Joe") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 105) {
                joeExists = true;
            }
        }
        assertTrue("Nigel not present", nigelExists);
        assertTrue("Joe not present", joeExists);
        tx.rollback();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) ManagedType(javax.persistence.metamodel.ManagedType) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Join(javax.persistence.criteria.Join) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) Metamodel(javax.persistence.metamodel.Metamodel)

Example 52 with CriteriaQuery

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

the class CriteriaStringsTest method testBasicWithFilterOr.

/**
 * Test basic generation of query with candidate and alias and filter (using OR).
 */
public void testBasicWithFilterOr() {
    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");
        Predicate firstName1 = cb.equal(firstNameField, "Fred");
        Predicate firstName2 = cb.equal(firstNameField, "Joe");
        Predicate eitherFirstName = cb.or(firstName1, firstName2);
        crit.where(eitherFirstName);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE" + " ((p.firstName = 'Fred') OR (p.firstName = 'Joe'))", crit.toString());
        Query q = em.createQuery(crit);
        List<Person> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 2, results.size());
        Iterator<Person> iter = results.iterator();
        boolean fredPresent = false;
        boolean joePresent = false;
        while (iter.hasNext()) {
            Person pers = iter.next();
            if (pers.getFirstName().equals("Fred") && pers.getLastName().equals("Flintstone") && pers.getPersonNum() == 101) {
                fredPresent = true;
            } else if (pers.getFirstName().equals("Joe") && pers.getLastName().equals("Bloggs") && pers.getPersonNum() == 105) {
                joePresent = true;
            }
        }
        assertTrue("Fred was not returned!", fredPresent);
        assertTrue("Joe was not returned!", joePresent);
        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) 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)

Example 53 with CriteriaQuery

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

the class CriteriaStringsTest method testBasic.

/**
 * Test basic generation of query with candidate and alias.
 */
public void testBasic() {
    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);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p", 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 54 with CriteriaQuery

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

the class CriteriaStringsTest method testFilterWithIn.

/**
 * Test generation of filter with IN.
 */
public void testFilterWithIn() {
    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);
        List<String> nameOptions = new ArrayList<String>();
        nameOptions.add("Fred");
        nameOptions.add("George");
        Path firstNameField = candidate.get("firstName");
        Predicate nameIn = firstNameField.in(nameOptions);
        crit.where(nameIn);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p FROM org.datanucleus.samples.annotations.models.company.Person p WHERE p.firstName IN ('Fred','George')", 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) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 55 with CriteriaQuery

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

the class CriteriaStringsTest method testBasicWithResult.

/**
 * Test basic generation of query with result.
 */
public void testBasicWithResult() {
    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");
        Path firstNameField = candidate.get("firstName");
        Path lastNameField = candidate.get("lastName");
        crit.multiselect(firstNameField, lastNameField);
        // DN extension
        assertEquals("Generated JPQL query is incorrect", "SELECT p.firstName,p.lastName FROM org.datanucleus.samples.annotations.models.company.Person p", crit.toString());
        Query q = em.createQuery(crit);
        List<Object> results = q.getResultList();
        assertNotNull("Null results returned!", results);
        assertEquals("Number of results is incorrect", 4, results.size());
        Iterator<Object> iter = results.iterator();
        boolean fredExists = false;
        boolean barneyExists = false;
        boolean joeExists = false;
        boolean nigelExists = false;
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Row of results is not Object[]", obj instanceof Object[]);
            Object[] row = (Object[]) obj;
            if (row[0].equals("Fred") && row[1].equals("Flintstone")) {
                fredExists = true;
            } else if (row[0].equals("Barney") && row[1].equals("Rubble")) {
                barneyExists = true;
            } else if (row[0].equals("Joe") && row[1].equals("Bloggs")) {
                joeExists = true;
            } else if (row[0].equals("Nigel") && row[1].equals("Bloggs")) {
                nigelExists = true;
            }
        }
        assertTrue("Fred wasnt in the results", fredExists);
        assertTrue("Barney wasnt in the results", barneyExists);
        assertTrue("Joe wasnt in the results", joeExists);
        assertTrue("Nigel wasnt in the results", nigelExists);
        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) EntityManager(javax.persistence.EntityManager) Person(org.datanucleus.samples.annotations.models.company.Person)

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