use of org.eclipse.persistence.internal.jpa.querydef.CompoundExpressionImpl in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testGetRestrictionReturningCorrectPredicate.
/**
* Bug 464833 - Criteria API: calling getRestriction() on a query returns Predicate with incorrect expression
* An incorrect expression is observed when calling getRestriction() on an existing query to obtain an existing Predicate.
* Tests: Validate that an existing Predicate (obtained with criteriaQuery.getRestriction()) has correct child expressions.
*/
public void testGetRestrictionReturningCorrectPredicate() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Employee> criteriaQuery = builder.createQuery(Employee.class);
Root<Employee> root = criteriaQuery.from(Employee.class);
// simple case - construct a predicate
criteriaQuery.where(builder.equal(root.get("firstName"), "Bob"));
TypedQuery<Employee> query1 = em.createQuery(criteriaQuery);
List<Employee> results1 = query1.getResultList();
long count1 = (Long) em.createQuery("select count(e) from Employee e where e.firstName = 'Bob'").getSingleResult();
// validate the expressions on the Predicate returned from CriteriaQuery getRestriction()
Predicate predicate = criteriaQuery.getRestriction();
// for the current example, the Predicate returned is expected to be a CompoundExpressionImpl
assertNotNull("Predicate should be non-null", predicate);
assertTrue("Invalid predicate type returned: " + predicate.getClass().getName(), predicate instanceof CompoundExpressionImpl);
CompoundExpressionImpl compoundExpression = (CompoundExpressionImpl) predicate;
// The where has two child expressions representing:
// 1) a path (query key) for "firstName" and 2) an expression (constant) for "Bob".
List<Expression<?>> expressions = compoundExpression.getChildExpressions();
assertSame("Predicate should have two child expressions", 2, expressions.size());
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
Aggregations