Search in sources :

Example 1 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class NamedParameterUtils method buildValueArray.

/**
	 * Convert a Map of named parameter values to a corresponding array.
	 * @param parsedSql the parsed SQL statement
	 * @param paramSource the source for named parameters
	 * @param declaredParams the List of declared SqlParameter objects
	 * (may be {@code null}). If specified, the parameter metadata will
	 * be built into the value array in the form of SqlParameterValue objects.
	 * @return the array of values
	 */
public static Object[] buildValueArray(ParsedSql parsedSql, SqlParameterSource paramSource, List<SqlParameter> declaredParams) {
    Object[] paramArray = new Object[parsedSql.getTotalParameterCount()];
    if (parsedSql.getNamedParameterCount() > 0 && parsedSql.getUnnamedParameterCount() > 0) {
        throw new InvalidDataAccessApiUsageException("Not allowed to mix named and traditional ? placeholders. You have " + parsedSql.getNamedParameterCount() + " named parameter(s) and " + parsedSql.getUnnamedParameterCount() + " traditional placeholder(s) in statement: " + parsedSql.getOriginalSql());
    }
    List<String> paramNames = parsedSql.getParameterNames();
    for (int i = 0; i < paramNames.size(); i++) {
        String paramName = paramNames.get(i);
        try {
            Object value = paramSource.getValue(paramName);
            SqlParameter param = findParameter(declaredParams, paramName, i);
            paramArray[i] = (param != null ? new SqlParameterValue(param, value) : value);
        } catch (IllegalArgumentException ex) {
            throw new InvalidDataAccessApiUsageException("No value supplied for the SQL parameter '" + paramName + "': " + ex.getMessage());
        }
    }
    return paramArray;
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 2 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-data-mongodb by spring-projects.

the class MongoRepositoryFactory method getRepositoryFragments.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getRepositoryFragments(org.springframework.data.repository.core.RepositoryMetadata)
	 */
@Override
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {
    RepositoryFragments fragments = RepositoryFragments.empty();
    boolean isQueryDslRepository = QUERY_DSL_PRESENT && QuerydslPredicateExecutor.class.isAssignableFrom(metadata.getRepositoryInterface());
    if (isQueryDslRepository) {
        if (metadata.isReactiveRepository()) {
            throw new InvalidDataAccessApiUsageException("Cannot combine Querydsl and reactive repository support in a single interface");
        }
        MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType(), metadata);
        fragments = fragments.append(RepositoryFragment.implemented(getTargetRepositoryViaReflection(QuerydslMongoPredicateExecutor.class, entityInformation, operations)));
    }
    return fragments;
}
Also used : RepositoryFragments(org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments) Serializable(java.io.Serializable) QuerydslPredicateExecutor(org.springframework.data.querydsl.QuerydslPredicateExecutor) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 3 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project summerb by skarpushin.

the class AbstractJdbcUpdate method compile.

// -------------------------------------------------------------------------
// Methods handling compilation issues
// -------------------------------------------------------------------------
/**
 * Compile this JdbcUpdate using provided parameters and meta data plus
 * other settings. This finalizes the configuration for this object and
 * subsequent attempts to compile are ignored. This will be implicitly
 * called the first time an un-compiled update is executed.
 *
 * @throws org.springframework.dao.InvalidDataAccessApiUsageException
 *             if the object hasn't been correctly initialized, for example
 *             if no DataSource has been provided
 */
public final synchronized void compile() throws InvalidDataAccessApiUsageException {
    if (!isCompiled()) {
        if (getTableName() == null) {
            throw new InvalidDataAccessApiUsageException("Table name is required");
        }
        try {
            this.jdbcTemplate.afterPropertiesSet();
        } catch (IllegalArgumentException ex) {
            throw new InvalidDataAccessApiUsageException(ex.getMessage());
        }
        compileInternal();
        this.compiled = true;
        if (logger.isDebugEnabled()) {
            logger.debug("JdbcUpdate for table [" + getTableName() + "] compiled");
        }
    }
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 4 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project irida by phac-nml.

the class AnalysisSubmissionServiceImpl method create.

/**
 * {@inheritDoc}
 */
@Override
@PreAuthorize("hasRole('ROLE_USER')")
public AnalysisSubmission create(AnalysisSubmission analysisSubmission) throws ConstraintViolationException, EntityExistsException {
    UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    User user = userRepository.loadUserByUsername(userDetails.getUsername());
    analysisSubmission.setSubmitter(user);
    try {
        return super.create(analysisSubmission);
    } catch (final InvalidDataAccessApiUsageException e) {
        // if the exception is because we're using unsaved properties, try to wrap the exception with a sane-er message.
        if (e.getCause() != null) {
            final Throwable primaryCause = e.getCause();
            if (primaryCause.getCause() != null && primaryCause.getCause() instanceof TransientPropertyValueException) {
                final TransientPropertyValueException propertyException = (TransientPropertyValueException) primaryCause.getCause();
                if (Objects.equals("namedParameters", propertyException.getPropertyName())) {
                    throw new UnsupportedOperationException("You must save the named properties *before* you use them in a submission.", e);
                }
            }
        }
        throw e;
    }
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(ca.corefacility.bioinformatics.irida.model.user.User) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) TransientPropertyValueException(org.hibernate.TransientPropertyValueException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project ma-core-public by infiniteautomation.

the class DaoUtils method getIdKey.

@Deprecated
private Object getIdKey(GeneratedKeyHolder keyHolder) {
    List<Map<String, Object>> keyList = keyHolder.getKeyList();
    if (keyList.size() == 0)
        return null;
    if (keyList.size() > 1)
        throw new InvalidDataAccessApiUsageException("Multiple key records returned from insert");
    Map<String, Object> keys = keyList.get(0);
    if (keys.size() == 0)
        return null;
    if (keys.size() == 1)
        return keys.values().iterator().next();
    // Look for "id"
    Object key = keys.get("id");
    if (key == null)
        key = keys.get("ID");
    if (key == null)
        throw new InvalidDataAccessApiUsageException("No obvious id field found in keys: " + keys);
    return key;
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Map(java.util.Map)

Aggregations

InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)39 ArrayList (java.util.ArrayList)7 Test (org.junit.jupiter.api.Test)7 SQLException (java.sql.SQLException)6 List (java.util.List)6 Map (java.util.Map)6 Document (org.bson.Document)6 Collections (java.util.Collections)5 DataAccessException (org.springframework.dao.DataAccessException)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 Arrays (java.util.Arrays)4 SqlParameter (org.springframework.jdbc.core.SqlParameter)4 StringUtils (org.springframework.util.StringUtils)4 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)3 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Statement (java.sql.Statement)2 DataSource (javax.sql.DataSource)2