Пример #1
0
  private void validateParameterBinding(Parameter parameter, Object value) {
    if (value == null || parameter.getParameterType() == null) {
      // nothing we can check
      return;
    }

    if (Collection.class.isInstance(value)
        && !Collection.class.isAssignableFrom(parameter.getParameterType())) {
      // we have a collection passed in where we are expecting a non-collection.
      // 		NOTE : this can happen in Hibernate's notion of "parameter list" binding
      // 		NOTE2 : the case of a collection value and an expected collection (if that can even
      // happen)
      //			will fall through to the main check.
      validateCollectionValuedParameterMultiBinding(parameter, (Collection) value);
    } else if (value.getClass().isArray()) {
      validateArrayValuedParameterBinding(parameter, value);
    } else {
      if (!parameter.getParameterType().isInstance(value)) {
        throw new IllegalArgumentException(
            String.format(
                "Parameter value [%s] did not match expected type [%s]",
                value, parameter.getParameterType().getName()));
      }
    }
  }
Пример #2
0
  /**
   * Returns whether the given {@link Query} contains named parameters.
   *
   * @param query
   * @return
   */
  public static boolean hasNamedParameter(Query query) {

    for (Parameter<?> parameter : query.getParameters()) {
      if (parameter.getName() != null) {
        return true;
      }
    }

    return false;
  }
Пример #3
0
 /** {@inheritDoc} */
 public Parameter<?> getParameter(String name) {
   if (name == null) {
     throw new IllegalArgumentException("Name of parameter to locate cannot be null");
   }
   for (Parameter parameter : parameters) {
     if (name.equals(parameter.getName())) {
       return parameter;
     }
   }
   throw new IllegalArgumentException("Unable to locate parameter named [" + name + "]");
 }
Пример #4
0
 private void validateCollectionValuedParameterMultiBinding(
     Parameter parameter, Collection value) {
   // validate the elements...
   for (Object element : value) {
     if (!parameter.getParameterType().isInstance(element)) {
       throw new IllegalArgumentException(
           String.format(
               "Parameter value element [%s] did not match expected type [%s]",
               element, parameter.getParameterType().getName()));
     }
   }
 }
Пример #5
0
 public TypedQuery<X> setParameter(Parameter<Date> param, Date value, TemporalType temporalType) {
   if (!parameters.contains(param)) {
     throw new IllegalArgumentException("Specified parameter was not found in query");
   }
   if (param.getName() != null) {
     // a named param, for not delegate out.  Eventually delegate *into* this method...
     setParameter(param.getName(), value, temporalType);
   } else {
     setParameter(param.getPosition(), value, temporalType);
   }
   return this;
 }
Пример #6
0
 /** {@inheritDoc} */
 public Parameter<?> getParameter(int position) {
   if (isJpaPositionalParameter(position)) {
     return getParameter(Integer.toString(position));
   } else {
     for (Parameter parameter : parameters) {
       if (parameter.getPosition() != null && position == parameter.getPosition()) {
         return parameter;
       }
     }
     throw new IllegalArgumentException(
         "Unable to locate parameter with position [" + position + "]");
   }
 }
Пример #7
0
  /**
   * Returns whether the given {@link Query} contains named parameters.
   *
   * @param query
   * @return
   */
  public static boolean hasNamedParameter(Query query) {

    for (Parameter<?> parameter : query.getParameters()) {

      String name = parameter.getName();

      // Hibernate 3 specific hack as it returns the index as String for the name.
      if (name != null && NO_DIGITS.matcher(name).find()) {
        return true;
      }
    }

    return false;
  }
Пример #8
0
 /** {@inheritDoc} */
 @SuppressWarnings({"unchecked"})
 public <T> Parameter<T> getParameter(int position, Class<T> type) {
   Parameter param = getParameter(position);
   if (param.getParameterType() != null) {
     // we were able to determine the expected type during analysis, so validate it here
     throw new IllegalArgumentException(
         "Parameter type ["
             + param.getParameterType().getName()
             + "] is not assignment compatible with requested type ["
             + type.getName()
             + "]");
   }
   return param;
 }
  private void setQueryParameters(Query query, SearchParameters sp) {
    // add default parameter if specified in the named query
    for (Parameter<?> p : query.getParameters()) {
      if (NAMED_PARAMETER_CURRENT_USER_ID.equals(p.getName())) {
        query.setParameter(NAMED_PARAMETER_CURRENT_USER_ID, UserContext.getId());
      } else if (NAMED_PARAMETER_NOW.equals(p.getName())) {
        query.setParameter(NAMED_PARAMETER_NOW, Calendar.getInstance().getTime());
      }
    }

    // add parameters for the named query
    for (String paramName : sp.getNamedQueryParameters().keySet()) {
      query.setParameter(paramName, sp.getNamedQueryParameter(paramName));
    }
  }
  @Override
  @SuppressWarnings("unchecked")
  public <T> Parameter<T> getParameter(String name, Class<T> type) {
    checkOpen(false);
    Parameter param = getParameter(name);

    if (param.getParameterType() != null) {
      // we were able to determine the expected type during analysis, so validate it here
      if (!param.getParameterType().isAssignableFrom(type)) {
        throw new IllegalArgumentException(
            String.format(
                "Parameter type [%s] is not assignment compatible with requested type [%s] for parameter named [%s]",
                param.getParameterType().getName(), type.getName(), name));
      }
    }
    return (Parameter<T>) param;
  }
  protected <X> ParameterRegistration<X> findParameterRegistration(Parameter<X> parameter) {
    if (ParameterRegistration.class.isInstance(parameter)) {
      final ParameterRegistration<X> reg = (ParameterRegistration<X>) parameter;
      // validate the parameter source
      if (reg.getQuery() != this) {
        throw new IllegalArgumentException("Passed Parameter was from different Query");
      }
      return reg;
    } else {
      if (parameter.getName() != null) {
        return findParameterRegistration(parameter.getName());
      } else if (parameter.getPosition() != null) {
        return findParameterRegistration(parameter.getPosition());
      }
    }

    throw new IllegalArgumentException(
        "Unable to resolve incoming parameter [" + parameter + "] to registration");
  }
Пример #12
0
  @Test
  public void testNamedParameterMetadata() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<MultiTypedBasicAttributesEntity> criteria =
        em.getCriteriaBuilder().createQuery(MultiTypedBasicAttributesEntity.class);
    Root<MultiTypedBasicAttributesEntity> rootEntity =
        criteria.from(MultiTypedBasicAttributesEntity.class);

    criteria.where(
        em.getCriteriaBuilder()
            .equal(
                rootEntity.get(MultiTypedBasicAttributesEntity_.id),
                em.getCriteriaBuilder().parameter(Long.class, "id")));

    TypedQuery<MultiTypedBasicAttributesEntity> query = em.createQuery(criteria);
    Parameter parameter = query.getParameter("id");
    assertEquals("id", parameter.getName());

    em.getTransaction().commit();
    em.close();
  }
Пример #13
0
  private void validateArrayValuedParameterBinding(Parameter parameter, Object value) {
    if (!parameter.getParameterType().isArray()) {
      throw new IllegalArgumentException(
          String.format(
              "Encountered array-valued parameter binding, but was expecting [%s]",
              parameter.getParameterType().getName()));
    }

    if (value.getClass().getComponentType().isPrimitive()) {
      // we have a primitive array.  we validate that the actual array has the component type (type
      // odf elements)
      // we expect based on the component type of the parameter specification
      if (!parameter
          .getParameterType()
          .getComponentType()
          .isAssignableFrom(value.getClass().getComponentType())) {
        throw new IllegalArgumentException(
            String.format(
                "Primitive array-valued parameter bind value type [%s] did not match expected type [%s]",
                value.getClass().getComponentType().getName(),
                parameter.getParameterType().getName()));
      }
    } else {
      // we have an object array.  Here we loop over the array and physically check each element
      // against
      // the type we expect based on the component type of the parameter specification
      final Object[] array = (Object[]) value;
      for (Object element : array) {
        if (!parameter.getParameterType().getComponentType().isInstance(element)) {
          throw new IllegalArgumentException(
              String.format(
                  "Array-valued parameter value element [%s] did not match expected type [%s]",
                  element, parameter.getParameterType().getName()));
        }
      }
    }
  }