Example usage for javax.persistence Query getParameters

List of usage examples for javax.persistence Query getParameters

Introduction

In this page you can find the example usage for javax.persistence Query getParameters.

Prototype

Set<Parameter<?>> getParameters();

Source Link

Document

Get the parameter objects corresponding to the declared parameters of the query.

Usage

From source file:net.sf.gazpachoquest.qbe.NamedQueryUtil.java

private void setQueryParameters(final Query query, final SearchParameters sp) {
    // add default parameter if specified in the named query
    for (Parameter<?> p : query.getParameters()) {
        if (NAMED_PARAMETER_NOW.equals(p.getName())) {
            query.setParameter(NAMED_PARAMETER_NOW, Calendar.getInstance().getTime());
        }/*from  w  w w  .  j a  va 2s  . c o  m*/
    }

    // add parameters for the named query
    for (String paramName : sp.getNamedQueryParameters().keySet()) {
        query.setParameter(paramName, sp.getNamedQueryParameter(paramName));
    }
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

@Transactional(propagation = Propagation.SUPPORTS)
protected Object findSingleResult(Query query) throws NotFoundException {
    try {//from  ww w .  j  a  va  2  s  .  com
        return query.getSingleResult();
    } catch (NoResultException e) {
        throw new NotFoundException("Result of " + query + " with parameters '"
                + Arrays.toString(query.getParameters().toArray()) + "' not found in database", e);
    }
}

From source file:org.jdal.dao.jpa.JpaDao.java

/**
 * Apply filter to parametriced Query//from   w w w  . java2 s .  c  o  m
 * @param query the query to apply filter on
 * @param filter Filter to apply
 */
private void applyFilter(Query query, Filter filter) {
    Map<String, Object> parameterMap = filter.getParameterMap();
    for (Parameter<?> p : query.getParameters()) {
        if (parameterMap.containsKey(p.getName())) {
            query.setParameter(p.getName(), parameterMap.get(p.getName()));
        } else {
            throw new InvalidDataAccessApiUsageException("Parameter " + p.getName() + "was not found in filter "
                    + filter.getFilterName() + ". Review NamedQuery or Filter.");
        }
    }
}

From source file:net.nan21.dnet.core.presenter.action.query.QueryBuilderWithJpql.java

private void bindFilterParams(Query q) throws Exception {

    if (this.defaultFilterItems != null) {
        for (String key : this.defaultFilterItems.keySet()) {
            Object value = this.defaultFilterItems.get(key);
            try {
                if (value instanceof java.lang.String) {
                    q.setParameter(key, ((String) value).replace('*', '%'));
                } else {
                    q.setParameter(key, value);
                }//from ww  w .ja  va  2s  .co  m
            } catch (IllegalArgumentException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug(e.getMessage());
                }
            }
        }
    }
    if (this.customFilterItems != null) {
        for (String key : this.customFilterItems.keySet()) {
            Object value = this.customFilterItems.get(key);
            if (value instanceof java.lang.String) {
                q.setParameter(key, ((String) value).replace('*', '%'));
            } else {
                q.setParameter(key, value);
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Bound filter params:");
        for (Parameter<?> p : q.getParameters()) {
            try {
                logger.debug(" -> " + p.getName() + " = " + q.getParameterValue(p));
            } catch (Exception e) {
                // maybe a parameter has not been bound
            }
        }
    }
}

From source file:nl.strohalm.cyclos.utils.database.DatabaseQueryHandler.java

/**
 * Sets the query bind named parameters/*from ww w .ja  v a  2s.c  o  m*/
 */
public void setQueryParameters(final Query query, final Object parameters) {
    if (parameters != null) {
        if (parameters instanceof Map<?, ?>) {
            final Map<?, ?> map = (Map<?, ?>) parameters;
            final String[] paramNames = new String[query.getParameters().size()];
            int i = 0;
            for (Parameter param : query.getParameters()) {
                paramNames[i] = param.getName();
                i++;
            }
            for (final String param : paramNames) {
                final Object value = map.get(param);
                if (value instanceof Collection<?>) {
                    final Collection<Object> values = new ArrayList<>(((Collection<?>) value).size());
                    for (final Object object : (Collection<?>) value) {
                        if (object instanceof EntityReference) {
                            values.add(fetchDao.fetch((Entity) object));
                        } else {
                            values.add(object);
                        }
                    }
                    query.setParameter(param, values);
                    //query.setParameterList(param, values);
                } else if (value instanceof EntityReference) {
                    query.setParameter(param, fetchDao.fetch((Entity) value));
                } else {
                    query.setParameter(param, value);
                }
            }
        } else {
            //query.setProperties(parameters);
        }
    }
}

From source file:org.debux.webmotion.jpa.GenericDAO.java

/**
 * Set parameter in the query./* w  w w. j  av a 2 s . co  m*/
 * 
 * @param query query
 * @param parameters parameters
 */
protected void extract(Query query, Parameters parameters) {
    Set<Parameter<?>> queryParameters = query.getParameters();
    for (Parameter<?> parameter : queryParameters) {
        String parameterName = parameter.getName();
        Object[] values = parameters.get(parameterName);

        List<Object> converted = Arrays.asList(values);
        query.setParameter(parameterName, converted);
    }
}

From source file:org.springframework.integration.jpa.core.DefaultJpaOperations.java

/**
 * Given a JPQL query, this method gets all parameters defined in this query and
 * use the {@link ParameterSource} to find their values and set them.
 *
 *///from w  w  w. j a  v a 2  s .  c  o m
private void setParametersIfRequired(String queryString, ParameterSource source, Query query) {
    Set<Parameter<?>> parameters = query.getParameters();

    if (parameters != null && !parameters.isEmpty()) {
        if (source != null) {
            for (Parameter<?> param : parameters) {
                String paramName = param.getName();
                Integer position = param.getPosition();

                final Object paramValue;

                if (position != null) {

                    if (source instanceof PositionSupportingParameterSource) {
                        paramValue = ((PositionSupportingParameterSource) source).getValueByPosition(position);
                        query.setParameter(position, paramValue);
                    } else {
                        throw new JpaOperationFailedException("Positional Parameters are only support "
                                + "for PositionSupportingParameterSources.", queryString);
                    }

                } else {

                    if (StringUtils.hasText(paramName)) {
                        paramValue = source.getValue(paramName);
                        query.setParameter(paramName, paramValue);
                    } else {
                        throw new JpaOperationFailedException(
                                "This parameter does not contain a parameter name. "
                                        + "Additionally it is not a positional parameter, neither.",
                                queryString);
                    }
                }

            }
        } else {
            throw new IllegalArgumentException("Query has parameters but no parameter source provided");
        }

    }
}