Search in sources :

Example 1 with Expression

use of jakarta.persistence.criteria.Expression in project hibernate-orm by hibernate.

the class InsertSubstringOverlayEmulation method generateSqmFunctionExpression.

@Override
protected <T> SelfRenderingSqmFunction<T> generateSqmFunctionExpression(List<? extends SqmTypedNode<?>> arguments, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
    final BasicType<Integer> intType = typeConfiguration.getBasicTypeForJavaType(Integer.class);
    final BasicType<String> stringType = typeConfiguration.getBasicTypeForJavaType(String.class);
    SqmTypedNode<?> string = arguments.get(0);
    SqmTypedNode<?> replacement = arguments.get(1);
    SqmTypedNode<?> start = arguments.get(2);
    SqmTypedNode<?> length = arguments.size() > 3 ? arguments.get(3) : queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("length").generateSqmExpression(replacement, intType, queryEngine, typeConfiguration);
    SqmFunctionDescriptor insert = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("insert");
    if (insert != null) {
        return insert.generateSqmExpression(asList(string, start, length, replacement), impliedResultType, queryEngine, typeConfiguration);
    } else {
        SqmFunctionDescriptor lengthFunction = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("length");
        SqmFunctionDescriptor substring = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("substring");
        SqmFunctionDescriptor concat = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("concat");
        SqmLiteral<Integer> one = new SqmLiteral<>(1, intType, queryEngine.getCriteriaBuilder());
        SqmExpression<Integer> startPlusLength = new SqmBinaryArithmetic<>(BinaryArithmeticOperator.ADD, (SqmExpression<?>) start, (SqmExpression<?>) length, intType, queryEngine.getCriteriaBuilder());
        SqmExpression<Integer> startMinusOne = new SqmBinaryArithmetic<>(BinaryArithmeticOperator.SUBTRACT, (SqmExpression<?>) start, one, intType, queryEngine.getCriteriaBuilder());
        SqmTypedNode<?> restString = substring.generateSqmExpression(asList(string, startPlusLength), impliedResultType, queryEngine, typeConfiguration);
        if (strictSubstring) {
            restString = new SqmCaseSearched<>(stringType, start.nodeBuilder()).when(new SqmComparisonPredicate(startPlusLength, ComparisonOperator.GREATER_THAN, lengthFunction.generateSqmExpression(asList(string), intType, queryEngine, typeConfiguration), string.nodeBuilder()), new SqmLiteral<>("", stringType, string.nodeBuilder())).otherwise((Expression<? extends String>) restString);
        }
        return concat.generateSqmExpression(asList(substring.generateSqmExpression(asList(string, one, startMinusOne), impliedResultType, queryEngine, typeConfiguration), replacement, restString), impliedResultType, queryEngine, typeConfiguration);
    }
}
Also used : SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) AbstractSqmFunctionDescriptor(org.hibernate.query.sqm.function.AbstractSqmFunctionDescriptor) SqmBinaryArithmetic(org.hibernate.query.sqm.tree.expression.SqmBinaryArithmetic) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) Expression(jakarta.persistence.criteria.Expression) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Example 2 with Expression

use of jakarta.persistence.criteria.Expression 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)

Example 3 with Expression

use of jakarta.persistence.criteria.Expression in project eclipselink by eclipse-ee4j.

the class CriteriaQueryTestSuite method testEqualsClauseSingleExpressionEmptyExpressionsList.

public void testEqualsClauseSingleExpressionEmptyExpressionsList() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
        Root<Employee> employee = query.from(Employee.class);
        EntityType<Employee> employeeModel = employee.getModel();
        Predicate predicate = criteriaBuilder.equal(employee.get(employeeModel.getSingularAttribute("firstName", String.class)), "Bob");
        List<Expression<Boolean>> expressions = predicate.getExpressions();
        assertTrue("An empty list should be returned", expressions.isEmpty());
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa21.advanced.Employee) Expression(jakarta.persistence.criteria.Expression) Predicate(jakarta.persistence.criteria.Predicate)

Example 4 with Expression

use of jakarta.persistence.criteria.Expression in project eclipselink by eclipse-ee4j.

the class CriteriaQueryTestSuite method testInClauseSingleExpressionEmptyExpressionsList.

public void testInClauseSingleExpressionEmptyExpressionsList() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaDelete<Employee> criteriaDelete = criteriaBuilder.createCriteriaDelete(Employee.class);
        Root<Employee> employee = criteriaDelete.from(Employee.class);
        CriteriaDelete<Employee> where = criteriaDelete.where(employee.get("firstName").in(Arrays.asList("Bob", "Bobby")));
        Predicate restriction = where.getRestriction();
        List<Expression<Boolean>> expressions = restriction.getExpressions();
        assertTrue("An empty list should be returned", expressions.isEmpty());
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa22.advanced.Employee) Expression(jakarta.persistence.criteria.Expression) Predicate(jakarta.persistence.criteria.Predicate)

Example 5 with Expression

use of jakarta.persistence.criteria.Expression in project eclipselink by eclipse-ee4j.

the class QueryTestSuite method testCriteriaGetGroupList.

public void testCriteriaGetGroupList() {
    EntityManager em = createEntityManager();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> query = qb.createQuery(Employee.class);
    List<Expression<?>> groupList = query.getGroupList();
    assertNotNull("getGroupList returned null.", groupList);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa22.advanced.Employee) Expression(jakarta.persistence.criteria.Expression)

Aggregations

Expression (jakarta.persistence.criteria.Expression)17 EntityManager (jakarta.persistence.EntityManager)9 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)9 Predicate (jakarta.persistence.criteria.Predicate)9 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)6 ParameterExpression (jakarta.persistence.criteria.ParameterExpression)5 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)4 SubSelectExpression (org.eclipse.persistence.internal.expressions.SubSelectExpression)4 Employee (org.eclipse.persistence.testing.models.jpa21.advanced.Employee)4 Employee (org.eclipse.persistence.testing.models.jpa22.advanced.Employee)4 ArrayList (java.util.ArrayList)3 Root (jakarta.persistence.criteria.Root)1 Collection (java.util.Collection)1 ArgumentListFunctionExpression (org.eclipse.persistence.internal.expressions.ArgumentListFunctionExpression)1 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)1 MetamodelImpl (org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl)1 TypeImpl (org.eclipse.persistence.internal.jpa.metamodel.TypeImpl)1 CompoundExpressionImpl (org.eclipse.persistence.internal.jpa.querydef.CompoundExpressionImpl)1 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)1 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)1