Search in sources :

Example 1 with CompoundExpressionImpl

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);
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Predicate(jakarta.persistence.criteria.Predicate) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) ParameterExpression(jakarta.persistence.criteria.ParameterExpression) Expression(jakarta.persistence.criteria.Expression) CompoundExpressionImpl(org.eclipse.persistence.internal.jpa.querydef.CompoundExpressionImpl)

Aggregations

EntityManager (jakarta.persistence.EntityManager)1 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)1 Expression (jakarta.persistence.criteria.Expression)1 ParameterExpression (jakarta.persistence.criteria.ParameterExpression)1 Predicate (jakarta.persistence.criteria.Predicate)1 CompoundExpressionImpl (org.eclipse.persistence.internal.jpa.querydef.CompoundExpressionImpl)1 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)1 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)1