Example usage for org.springframework.integration.jpa.core JpaOperationFailedException JpaOperationFailedException

List of usage examples for org.springframework.integration.jpa.core JpaOperationFailedException JpaOperationFailedException

Introduction

In this page you can find the example usage for org.springframework.integration.jpa.core JpaOperationFailedException JpaOperationFailedException.

Prototype

public JpaOperationFailedException(String message, String offendingJPAQ1) 

Source Link

Usage

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 .  ja v  a2  s. co  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");
        }

    }
}