Search in sources :

Example 11 with Expression

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

the class CriteriaQueryTestSuite method testEqualsClauseExpressionConjunctionNonEmptyExpressionsList.

public void testEqualsClauseExpressionConjunctionNonEmptyExpressionsList() {
    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 predicate1 = criteriaBuilder.equal(employee.get(employeeModel.getSingularAttribute("firstName", String.class)), "Bob");
        Predicate predicate2 = criteriaBuilder.equal(employee.get(employeeModel.getSingularAttribute("firstName", String.class)), "Bobby");
        Predicate predicate = criteriaBuilder.and(predicate1, predicate2);
        List<Expression<Boolean>> expressions = predicate.getExpressions();
        assertEquals("Two expressions should be returned", 2, expressions.size());
    } 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 12 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.jpa21.advanced.Employee) Expression(jakarta.persistence.criteria.Expression)

Example 13 with Expression

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

the class CriteriaQueryTestSuite method testEqualsClauseExpressionConjunctionNonEmptyExpressionsList.

public void testEqualsClauseExpressionConjunctionNonEmptyExpressionsList() {
    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 predicate1 = criteriaBuilder.equal(employee.get(employeeModel.getSingularAttribute("firstName", String.class)), "Bob");
        Predicate predicate2 = criteriaBuilder.equal(employee.get(employeeModel.getSingularAttribute("firstName", String.class)), "Bobby");
        Predicate predicate = criteriaBuilder.and(predicate1, predicate2);
        List<Expression<Boolean>> expressions = predicate.getExpressions();
        assertEquals("Two expressions should be returned", 2, expressions.size());
    } 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 14 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.jpa22.advanced.Employee) Expression(jakarta.persistence.criteria.Expression) Predicate(jakarta.persistence.criteria.Predicate)

Example 15 with Expression

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

the class CriteriaQueryImpl method createSimpleQuery.

protected ObjectLevelReadQuery createSimpleQuery() {
    ObjectLevelReadQuery query = null;
    if (this.queryResult == ResultType.UNKNOWN) {
        // unknown type so let's figure this out.
        if (selection == null) {
            if (this.roots != null && !this.roots.isEmpty()) {
                this.selection = (SelectionImpl<?>) this.roots.iterator().next();
                query = new ReadAllQuery(((FromImpl) this.selection).getJavaType());
                List<org.eclipse.persistence.expressions.Expression> list = ((FromImpl) this.roots.iterator().next()).findJoinFetches();
                for (org.eclipse.persistence.expressions.Expression fetch : list) {
                    query.addJoinedAttribute(fetch);
                }
                if (!list.isEmpty()) {
                    query.setExpressionBuilder(list.get(0).getBuilder());
                }
            } else if (this.roots == null || this.roots.isEmpty()) {
                throw new IllegalArgumentException(ExceptionLocalization.buildMessage("CRITERIA_NO_ROOT_FOR_COMPOUND_QUERY"));
            }
        } else {
            // Selection is not null set type to selection
            TypeImpl type = ((MetamodelImpl) this.metamodel).getType(selection.getJavaType());
            if (type != null && type.getPersistenceType().equals(PersistenceType.ENTITY)) {
                query = new ReadAllQuery(type.getJavaType());
                List<org.eclipse.persistence.expressions.Expression> list = ((FromImpl) this.roots.iterator().next()).findJoinFetches();
                for (org.eclipse.persistence.expressions.Expression fetch : list) {
                    query.addJoinedAttribute(fetch);
                }
                query.setExpressionBuilder(((InternalSelection) selection).getCurrentNode().getBuilder());
            } else {
                query = new ReportQuery();
                query.setReferenceClass(this.selection.getCurrentNode().getBuilder().getQueryClass());
                if (!this.selection.isCompoundSelection() && ((InternalExpression) this.selection).isCompoundExpression()) {
                    if (((FunctionExpressionImpl) this.selection).getOperation() == CriteriaBuilderImpl.SIZE) {
                        // selecting size not all databases support subselect in select clause so convert to count/groupby
                        PathImpl collectionExpression = (PathImpl) ((FunctionExpressionImpl) this.selection).getChildExpressions().get(0);
                        ExpressionImpl fromExpression = (ExpressionImpl) collectionExpression.getParentPath();
                        ((ReportQuery) query).addAttribute(this.selection.getAlias(), collectionExpression.getCurrentNode().count(), ClassConstants.INTEGER);
                        ((ReportQuery) query).addGrouping(fromExpression.getCurrentNode());
                    }
                    ((ReportQuery) query).addAttribute(this.selection.getAlias(), this.selection.getCurrentNode(), this.selection.getJavaType());
                } else {
                    ((ReportQuery) query).addItem(this.selection.getAlias(), this.selection.getCurrentNode());
                    ((ReportQuery) query).setShouldReturnSingleAttribute(true);
                }
            }
        }
    } else if (this.queryResult.equals(ResultType.ENTITY)) {
        if (this.selection != null && (!((InternalSelection) this.selection).isRoot())) {
            query = new ReportQuery();
            query.setReferenceClass(this.queryType);
            ((ReportQuery) query).addItem(this.selection.getAlias(), this.selection.getCurrentNode(), ((FromImpl) this.selection).findJoinFetches());
            ((ReportQuery) query).setShouldReturnSingleAttribute(true);
        } else {
            query = new ReadAllQuery(this.queryType);
            if (this.roots != null && !this.roots.isEmpty()) {
                List<org.eclipse.persistence.expressions.Expression> list = ((FromImpl) this.roots.iterator().next()).findJoinFetches();
                if (!list.isEmpty()) {
                    // set the builder to one of the fetches bases.
                    query.setExpressionBuilder(list.get(0).getBuilder());
                }
                for (org.eclipse.persistence.expressions.Expression fetch : list) {
                    query.addJoinedAttribute(fetch);
                }
            }
            if (selection != null) {
                query.setExpressionBuilder(this.selection.currentNode.getBuilder());
            }
        }
    } else {
        ReportQuery reportQuery = null;
        if (this.queryResult.equals(ResultType.TUPLE)) {
            List<Object> list = new ArrayList<>();
            list.add(this.selection);
            reportQuery = new TupleQuery(list);
        } else {
            reportQuery = new ReportQuery();
            reportQuery.setShouldReturnWithoutReportQueryResult(true);
        }
        if (this.selection != null) {
            if (!this.selection.isCompoundSelection() && ((InternalExpression) this.selection).isCompoundExpression()) {
                if (((FunctionExpressionImpl) this.selection).getOperation() == CriteriaBuilderImpl.SIZE) {
                    // selecting size not all databases support subselect in select clause so convert to count/groupby
                    PathImpl collectionExpression = (PathImpl) ((FunctionExpressionImpl) this.selection).getChildExpressions().get(0);
                    ExpressionImpl fromExpression = (ExpressionImpl) collectionExpression.getParentPath();
                    reportQuery.addAttribute(this.selection.getAlias(), collectionExpression.getCurrentNode().count(), ClassConstants.INTEGER);
                    reportQuery.addGrouping(fromExpression.getCurrentNode());
                } else {
                    reportQuery.addAttribute(this.selection.getAlias(), this.selection.getCurrentNode(), this.selection.getJavaType());
                }
            } else {
                if (((InternalSelection) selection).isFrom()) {
                    reportQuery.addItem(selection.getAlias(), selection.getCurrentNode(), ((FromImpl) selection).findJoinFetches());
                } else {
                    reportQuery.addAttribute(selection.getAlias(), selection.getCurrentNode(), selection.getJavaType());
                }
            }
            reportQuery.setReferenceClass(((InternalSelection) this.selection).getCurrentNode().getBuilder().getQueryClass());
            reportQuery.setExpressionBuilder(((InternalSelection) this.selection).getCurrentNode().getBuilder());
        }
        query = reportQuery;
        if (this.groupBy != null && !this.groupBy.isEmpty()) {
            for (Expression<?> exp : this.groupBy) {
                reportQuery.addGrouping(((InternalSelection) exp).getCurrentNode());
            }
        }
        if (this.havingClause != null) {
            reportQuery.setHavingExpression(((InternalSelection) this.havingClause).getCurrentNode());
        }
    }
    if (query.getReferenceClass() == null) {
        if (this.where != null && ((InternalSelection) this.where).getCurrentNode() != null && ((InternalSelection) this.where).getCurrentNode().getBuilder() != null && ((InternalSelection) this.where).getCurrentNode().getBuilder().getQueryClass() != null) {
            query.setReferenceClass(((InternalSelection) this.where).getCurrentNode().getBuilder().getQueryClass());
        } else if (roots != null && !roots.isEmpty()) {
            Root<?> root = this.getRoots().iterator().next();
            query.setReferenceClass(root.getJavaType());
        }
    }
    if (selection == null) {
        // the builder in the where clause  may not be the correct builder for this query.  Search for a root that matches the query type.
        if (roots != null && !roots.isEmpty()) {
            for (Root root : this.getRoots()) {
                if (root.getJavaType().equals(this.queryType)) {
                    query.setExpressionBuilder(((RootImpl) root).getCurrentNode().getBuilder());
                    break;
                }
            }
        }
    }
    return query;
}
Also used : Root(jakarta.persistence.criteria.Root) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) MetamodelImpl(org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl) Expression(jakarta.persistence.criteria.Expression) TypeImpl(org.eclipse.persistence.internal.jpa.metamodel.TypeImpl)

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