use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaUnitTestSuite method testMaxAndFirstResultsOnObjectQueryOnInheritanceRoot.
/**
* Tests fix for bug 253258 that using filtering using MaxResults/FirstResult returns
* the correct number of results on an inheritance root class.
*/
public void testMaxAndFirstResultsOnObjectQueryOnInheritanceRoot() {
Exception exception = null;
List resultList = null;
EntityManager em = createEntityManager();
beginTransaction(em);
try {
clearCache();
// "SELECT p FROM Project p"
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Project> cq = qb.createQuery(Project.class);
Query query = em.createQuery(cq);
try {
query.setFirstResult(6);
query.setMaxResults(1);
resultList = query.getResultList();
} catch (Exception e) {
exception = e;
logThrowable(exception);
}
Assert.assertNull("Exception was caught.", 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 AdvancedCriteriaQueryTestSuite method testSimpleWhereObject.
public void testSimpleWhereObject() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
CriteriaBuilder qb = em.getCriteriaBuilder();
Root<Employee> root = cq.from(em.getMetamodel().entity(Employee.class));
cq.where(qb.equal(root.get("firstName"), qb.literal("Bob")));
TypedQuery<Employee> tq = em.createQuery(cq);
List<Employee> result = tq.getResultList();
assertFalse("No Employees were returned", result.isEmpty());
assertTrue("Did not return Employee", result.get(0).getClass().equals(Employee.class));
assertTrue("Employee had wrong firstname", result.get(0).getFirstName().equalsIgnoreCase("bob"));
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testInCollectionEntity.
public void testInCollectionEntity() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
Root<Employee> emp = cq.from(Employee.class);
Root<PhoneNumber> phone = cq.from(PhoneNumber.class);
cq.where(qb.and(qb.equal(phone.get("areaCode"), "613"), phone.in(emp.<Collection<?>>get("phoneNumbers"))));
Query query = em.createQuery(cq);
List<Employee> result = query.getResultList();
assertFalse("No Employees were returned", result.isEmpty());
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testQueryCacheFirstCacheHits.
/**
* Test that a cache hit will occur on a query.
*/
public void testQueryCacheFirstCacheHits() {
EntityManager em = createEntityManager();
beginTransaction(em);
QuerySQLTracker counter = null;
try {
// Load an employee into the cache.
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
Query query = em.createQuery(cq);
List result = query.getResultList();
Employee employee = (Employee) result.get(result.size() - 1);
// Count SQL.
counter = new QuerySQLTracker(getServerSession());
// Query by primary key.
cq = qb.createQuery(Employee.class);
Root<Employee> from = cq.from(Employee.class);
cq.where(qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName")));
query = em.createQuery(cq);
query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheThenDatabase);
query.setParameter("firstName", employee.getFirstName());
Employee queryResult = (Employee) query.getSingleResult();
if (!queryResult.getFirstName().equals(employee.getFirstName())) {
fail("Employees are not equal: " + employee + ", " + queryResult);
}
if (counter.getSqlStatements().size() > 0) {
fail("Cache hit do not occur: " + counter.getSqlStatements());
}
} finally {
rollbackTransaction(em);
if (counter != null) {
counter.remove();
}
}
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testTupleQuery.
/**
* Test that a cache hit will occur on a primary key query.
*/
public void testTupleQuery() {
EntityManager em = createEntityManager();
QuerySQLTracker counter = null;
beginTransaction(em);
try {
// Load an employee into the cache.
CriteriaBuilder qb = em.getCriteriaBuilder();
Query query = em.createQuery(em.getCriteriaBuilder().createQuery(Employee.class));
List result = query.getResultList();
Employee employee = (Employee) result.get(0);
// Count SQL.
counter = new QuerySQLTracker(getServerSession());
// Query by primary key.
CriteriaQuery<Tuple> cq = qb.createQuery(Tuple.class);
Root<Employee> from = cq.from(Employee.class);
cq.multiselect(from.get("id"), from.get("firstName"));
cq.where(qb.and(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")), qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName"))));
TypedQuery<Tuple> typedQuery = em.createQuery(cq);
typedQuery.setParameter("id", employee.getId());
typedQuery.setParameter("firstName", employee.getFirstName());
Tuple queryResult = typedQuery.getSingleResult();
assertTrue("Query Results do not match selection", queryResult.get(0).equals(employee.getId()) && queryResult.get(1).equals(employee.getFirstName()));
} finally {
rollbackTransaction(em);
if (counter != null) {
counter.remove();
}
}
}
Aggregations