use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method multipleExecutionOfCriteriaQueryTest.
public void multipleExecutionOfCriteriaQueryTest() {
// bug 5279859
EntityManager em = createEntityManager();
beginTransaction(em);
try {
// "SELECT e FROM Employee e where e.address.postalCode = :postalCode"
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
cq.where(qb.equal(wrapper.get((Path) wrapper.get(wrapper.from(cq, Employee.class), Employee_address), Address_postalCode), qb.parameter(String.class, "postalCode")));
Query query = em.createQuery(cq);
query.setParameter("postalCode", "K1T3B9");
try {
query.getResultList();
} catch (RuntimeException ex) {
fail("Failed to execute query, exception resulted on first execution, not expected");
}
try {
query.getResultList();
} catch (RuntimeException ex) {
fail("Failed to execute query, exception resulted on second execution");
}
query = em.createNamedQuery("findEmployeeByPostalCode", Employee.class);
query.setParameter("postalCode", "K1T3B9");
try {
query.getResultList();
} catch (RuntimeException ex) {
fail("Failed to execute query, exception resulted on first execution, of second use of named query");
}
query.setMaxResults(100000);
try {
query.getResultList();
} catch (RuntimeException ex) {
fail("Failed to execute query, exception resulted after setting max results (forcing reprepare)");
}
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testMaxAndFirstResultsOnDataQueryWithGroupBy.
/**
* Tests fix for bug6070214 that using Oracle Rownum pagination with group by
* throws an SQl exception.
*/
public void testMaxAndFirstResultsOnDataQueryWithGroupBy() {
Exception exception = null;
List resultList = null;
EntityManager em = createEntityManager();
beginTransaction(em);
try {
clearCache();
// "SELECT e.id FROM Employee e group by e.id"
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Integer> cq = qb.createQuery(Integer.class);
Root<Employee> rootEmp = cq.from(Employee.class);
Join<Object, Object> joinedManager = rootEmp.join("manager", JoinType.LEFT);
cq.select(rootEmp.get("id"));
Query query = em.createQuery(cq);
try {
query.setFirstResult(1);
query.setMaxResults(1);
resultList = query.getResultList();
} catch (Exception e) {
logThrowable(exception);
exception = e;
}
Assert.assertNull("Exception was caught: " + exception, exception);
Assert.assertTrue("Incorrect number of results returned. Expected 1, returned " + resultList.size(), resultList.size() == 1);
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testNonExistentConstructorCriteriaQuery.
public void testNonExistentConstructorCriteriaQuery() {
Exception expectedException = null;
EntityManager em = createEntityManager();
CriteriaBuilder qb = em.getCriteriaBuilder();
// Test constructor object:
CriteriaQuery<?> cq = qb.createQuery(DataHolder.class);
Root<Employee> from = cq.from(Employee.class);
EntityType<Employee> Emp_ = em.getMetamodel().entity(Employee.class);
// IllegalArgumentException expected from multiselect since DataHolder(String) does not exist
try {
cq.multiselect(from.get(Emp_.getSingularAttribute("lastName", String.class)));
Query query = em.createQuery(cq);
} catch (IllegalArgumentException exception) {
expectedException = exception;
}
assertNotNull("Expected IllegalArgumentException not thrown when using a non-existing constructor", expectedException);
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testIsNullAndIsNotNull.
public void testIsNullAndIsNotNull() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Employee emp = (Employee) em.createQuery("select e from Employee e where e.firstName = 'John' and e.lastName = 'Way'").getSingleResult();
emp.setFirstName(null);
emp = (Employee) em.createQuery("select e from Employee e where e.firstName = 'Charles' and e.lastName = 'Chanley'").getSingleResult();
emp.setFirstName(null);
emp.setLastName(null);
em.flush();
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<?> cq = qb.createQuery(Employee.class);
Root<Employee> from = cq.from(Employee.class);
EntityType<Employee> Emp_ = em.getMetamodel().entity(Employee.class);
List<Predicate> criteriaList = new ArrayList<Predicate>();
criteriaList.add(qb.isNotNull(from.get(Emp_.getSingularAttribute("lastName", String.class))));
criteriaList.add(qb.isNull(from.get(Emp_.getSingularAttribute("firstName", String.class))));
Predicate criteria = qb.and(criteriaList.toArray(new Predicate[0]));
cq.where(criteria);
Query query = em.createQuery(cq);
List results = query.getResultList();
assertTrue(results.size() == 1);
emp = (Employee) results.get(0);
assertTrue(emp.getFirstName() == null);
assertTrue(emp.getLastName() != null);
} finally {
rollbackTransaction(em);
}
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testMaxAndFirstResultsOnDataQuery.
/**
* Tests fix for bug6070214 that using Oracle Rownum pagination with non-unique columns
* throws an SQl exception.
*/
public void testMaxAndFirstResultsOnDataQuery() {
Exception exception = null;
List resultList = null;
EntityManager em = createEntityManager();
beginTransaction(em);
try {
clearCache();
// "SELECT e.id, m.id FROM Employee e LEFT OUTER JOIN e.manager m"
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = qb.createTupleQuery();
Root<Employee> rootEmp = cq.from(Employee.class);
Join<Object, Object> joinedManager = rootEmp.join("manager", JoinType.LEFT);
cq.multiselect(rootEmp.get("id"), joinedManager.get("id"));
Query query = em.createQuery(cq);
try {
query.setFirstResult(1);
query.setMaxResults(1);
resultList = query.getResultList();
} catch (Exception e) {
logThrowable(exception);
exception = e;
}
Assert.assertNull("Exception was caught: " + exception, exception);
Assert.assertTrue("Incorrect number of results returned. Expected 1, returned " + resultList.size(), resultList.size() == 1);
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
Aggregations