예제 #1
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;
 }
  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));
    }
  }
예제 #3
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;
  }
  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");
  }
예제 #5
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 + "]");
 }
예제 #6
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;
  }
  @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();
  }