Search in sources :

Example 1 with ParamNotSetException

use of com.querydsl.core.types.ParamNotSetException in project querydsl by querydsl.

the class JPAUtil method setConstants.

public static void setConstants(Query query, Map<Object, String> constants, Map<ParamExpression<?>, Object> params) {
    boolean hasParameters = !query.getParameters().isEmpty();
    for (Map.Entry<Object, String> entry : constants.entrySet()) {
        String key = entry.getValue();
        Object val = entry.getKey();
        if (Param.class.isInstance(val)) {
            val = params.get(val);
            if (val == null) {
                throw new ParamNotSetException((Param<?>) entry.getKey());
            }
        }
        if (hasParameters) {
            Parameter parameter = query.getParameter(Integer.parseInt(key));
            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(Integer.valueOf(key), val);
    }
}
Also used : ParamNotSetException(com.querydsl.core.types.ParamNotSetException) Parameter(javax.persistence.Parameter) Map(java.util.Map)

Aggregations

ParamNotSetException (com.querydsl.core.types.ParamNotSetException)1 Map (java.util.Map)1 Parameter (javax.persistence.Parameter)1