public static void setConstants(Query query, List<Object> constants, Map<ParamExpression<?>, Object> params) {
        boolean hasParameters = !query.getParameters().isEmpty();

        for (int i = 0; i < constants.size(); i++) {
            Object val = constants.get(i);

            if (val instanceof Param) {
                Param<?> param = (Param<?>) val;
                val = params.get(val);
                if (val == null) {
                    throw new ParamNotSetException(param);
                }
            }

            if (hasParameters) {
                Parameter parameter = query.getParameter(i + 1);
                Class parameterType = parameter != null ? parameter.getParameterType() : null;
                if (parameterType != null && !parameterType.isInstance(val)) {
                    if (val instanceof Number && Number.class.isAssignableFrom(parameterType)) {
                        val = MathUtils.cast((Number) val, parameterType);
                    }
                }
            }

            query.setParameter(i + 1, val);
        }
    }
JPAUtil provides static utility methods for JPA

@author tiwe
querydsl-jpa/src/main/java/com/querydsl/jpa/impl/JPAUtil.java