Search in sources :

Example 6 with Expression

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

the class CoalesceExpression method render.

public String render(RenderingContext renderingContext) {
    StringBuilder buffer = new StringBuilder("coalesce(");
    String sep = "";
    for (Expression expression : getExpressions()) {
        buffer.append(sep).append(((Renderable) expression).render(renderingContext));
        sep = ", ";
    }
    return buffer.append(")").toString();
}
Also used : Expression(javax.persistence.criteria.Expression)

Example 7 with Expression

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

the class InPredicate method render.

@Override
public String render(boolean isNegated, RenderingContext renderingContext) {
    final StringBuilder buffer = new StringBuilder();
    final Expression exp = getExpression();
    if (ParameterExpressionImpl.class.isInstance(exp)) {
        // technically we only need to CAST (afaik) if expression and all values are parameters.
        // but checking for that condition could take long time on a lon value list
        final ParameterExpressionImpl parameterExpression = (ParameterExpressionImpl) exp;
        final SessionFactoryImplementor sfi = criteriaBuilder().getEntityManagerFactory().unwrap(SessionFactoryImplementor.class);
        final Type mappingType = sfi.getTypeResolver().heuristicType(parameterExpression.getParameterType().getName());
        buffer.append("cast(").append(parameterExpression.render(renderingContext)).append(" as ").append(mappingType.getName()).append(")");
    } else {
        buffer.append(((Renderable) getExpression()).render(renderingContext));
    }
    if (isNegated) {
        buffer.append(" not");
    }
    buffer.append(" in ");
    // subquery expressions are already wrapped in parenthesis, so we only need to
    // render the parenthesis here if the values represent an explicit value list
    boolean isInSubqueryPredicate = getValues().size() == 1 && Subquery.class.isInstance(getValues().get(0));
    if (isInSubqueryPredicate) {
        buffer.append(((Renderable) getValues().get(0)).render(renderingContext));
    } else {
        buffer.append('(');
        String sep = "";
        for (Expression value : getValues()) {
            buffer.append(sep).append(((Renderable) value).render(renderingContext));
            sep = ", ";
        }
        buffer.append(')');
    }
    return buffer.toString();
}
Also used : Type(org.hibernate.type.Type) LiteralExpression(org.hibernate.query.criteria.internal.expression.LiteralExpression) Expression(javax.persistence.criteria.Expression) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ParameterExpressionImpl(org.hibernate.query.criteria.internal.expression.ParameterExpressionImpl) Subquery(javax.persistence.criteria.Subquery)

Example 8 with Expression

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

the class ParameterizedFunctionExpression method renderArguments.

protected void renderArguments(StringBuilder buffer, RenderingContext renderingContext) {
    String sep = "";
    for (Expression argument : argumentExpressions) {
        buffer.append(sep).append(((Renderable) argument).render(renderingContext));
        sep = ", ";
    }
}
Also used : Expression(javax.persistence.criteria.Expression)

Example 9 with Expression

use of javax.persistence.criteria.Expression in project tomee by apache.

the class QueryProxy method createFinderQuery.

private <T> Query createFinderQuery(final EntityManager entityManager, final String methodName, final Class<T> entityType, final Object[] args) {
    final List<String> conditions = parseMethodName(methodName);
    final EntityType<T> et = entityManager.getMetamodel().entity(entityType);
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object> query = cb.createQuery();
    final Root<T> from = query.from(entityType);
    query = query.select(from);
    int i = 0;
    Predicate where = null;
    for (final String condition : conditions) {
        final SingularAttribute<? super T, ?> attribute = et.getSingularAttribute(condition);
        final Path<?> path = from.get(attribute);
        final Class<?> javaType = attribute.getType().getJavaType();
        final Predicate currentClause;
        if (javaType.equals(String.class)) {
            currentClause = cb.like((Expression<String>) path, (String) args[i++]);
        } else if (Number.class.isAssignableFrom(javaType) || javaType.isPrimitive()) {
            currentClause = cb.equal(path, args[i++]);
        } else {
            LOGGER.warning("field " + condition + " not found, ignoring");
            continue;
        }
        if (where == null) {
            where = currentClause;
        } else {
            where = cb.and(where, currentClause);
        }
    }
    if (where != null) {
        query = query.where(where);
    }
    // pagination
    final TypedQuery<?> emQuery = entityManager.createQuery(query);
    if (args != null && args.length == conditions.size() + 2 && isInt(args[args.length - 2].getClass()) && isInt(args[args.length - 1].getClass())) {
        final int first = (Integer) args[args.length - 2];
        final int max = (Integer) args[args.length - 1];
        emQuery.setFirstResult(first);
        emQuery.setMaxResults(max);
    }
    return emQuery;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate) Expression(javax.persistence.criteria.Expression)

Aggregations

Expression (javax.persistence.criteria.Expression)9 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 ParameterExpression (javax.persistence.criteria.ParameterExpression)1 Path (javax.persistence.criteria.Path)1 Predicate (javax.persistence.criteria.Predicate)1 Subquery (javax.persistence.criteria.Subquery)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 Renderable (org.hibernate.query.criteria.internal.Renderable)1 LiteralExpression (org.hibernate.query.criteria.internal.expression.LiteralExpression)1 ParameterExpressionImpl (org.hibernate.query.criteria.internal.expression.ParameterExpressionImpl)1 SingularAttributePath (org.hibernate.query.criteria.internal.path.SingularAttributePath)1 Type (org.hibernate.type.Type)1