Example usage for org.springframework.dao IncorrectResultSizeDataAccessException IncorrectResultSizeDataAccessException

List of usage examples for org.springframework.dao IncorrectResultSizeDataAccessException IncorrectResultSizeDataAccessException

Introduction

In this page you can find the example usage for org.springframework.dao IncorrectResultSizeDataAccessException IncorrectResultSizeDataAccessException.

Prototype

public IncorrectResultSizeDataAccessException(int expectedSize) 

Source Link

Document

Constructor for IncorrectResultSizeDataAccessException.

Usage

From source file:org.mitre.util.jpa.JpaUtil.java

public static <T> T getSingleResult(List<T> list) {
    switch (list.size()) {
    case 0:/*from  w w  w .j a  v a2s .  c o  m*/
        return null;
    case 1:
        return list.get(0);
    default:
        throw new IncorrectResultSizeDataAccessException(1);
    }
}

From source file:org.javaweb.elasticsearch.core.ElasticsearchTemplate.java

/**
 * ?/*from  w w w  .j av a  2  s.co m*/
 *
 * @param searchRequest
 * @param clazz
 * @return
 * @throws IncorrectResultSizeDataAccessException
 */
@Override
public <T> T queryForObject(SearchRequest searchRequest, Class<T> clazz) {
    List<T> list = queryForList(searchRequest, clazz);
    if (list.size() > 1) {
        new IncorrectResultSizeDataAccessException(list.size()).printStackTrace();
    }
    return list.get(0);
}

From source file:com.ineunet.knife.persist.dao.support.JdbcDaoSupport.java

@Override
public Object[] queryForArray(String sql, int cols, Object... args) {
    SqlRowSet sqlSet = getJdbcTemplate().queryForRowSet(sql, args);
    Object[] array = null;//  w ww  .  java  2s  . com
    //sqlSet.getRow(): 0
    //sqlSet.isBeforeFirst(): true
    //sqlSet.isFirst(): false
    if (sqlSet.next()) {
        array = new Object[cols];
        for (int i = 0; i < cols; i++) {
            array[i] = sqlSet.getObject(i + 1);
        }
    }
    if (sqlSet.next()) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    //sqlSet.isFirst(): true
    //sqlSet.isLast(): true
    //sqlSet.isAfterLast(): false
    return array;
}

From source file:com.alibaba.cobar.client.CobarSqlMapClientTemplate.java

@Override
public Object queryForObject(final String statementName, final Object parameterObject,
        final Object resultObject) throws DataAccessException {
    auditSqlIfNecessary(statementName, parameterObject);
    long startTimestamp = System.currentTimeMillis();
    try {// w ww.  j  a  v  a  2  s .c  om
        if (isPartitioningBehaviorEnabled()) {
            SortedMap<String, DataSource> dsMap = lookupDataSourcesByRouter(statementName, parameterObject);
            if (!MapUtils.isEmpty(dsMap)) {
                SqlMapClientCallback callback = null;
                if (resultObject == null) {
                    callback = new SqlMapClientCallback() {
                        public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                            return executor.queryForObject(statementName, parameterObject);
                        }
                    };
                } else {
                    callback = new SqlMapClientCallback() {
                        public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                            return executor.queryForObject(statementName, parameterObject, resultObject);
                        }
                    };
                }
                List<Object> resultList = executeInConcurrency(callback, dsMap);
                @SuppressWarnings("unchecked")
                Collection<Object> filteredResultList = CollectionUtils.select(resultList, new Predicate() {
                    public boolean evaluate(Object item) {
                        return item != null;
                    }
                });
                if (filteredResultList.size() > 1) {
                    throw new IncorrectResultSizeDataAccessException(1);
                }
                if (CollectionUtils.isEmpty(filteredResultList)) {
                    return null;
                }
                return filteredResultList.iterator().next();
            }
        } // end if for partitioning status checking
        if (resultObject == null) {
            return super.queryForObject(statementName, parameterObject);
        } else {
            return super.queryForObject(statementName, parameterObject, resultObject);
        }
    } finally {
        if (isProfileLongTimeRunningSql()) {
            long interval = System.currentTimeMillis() - startTimestamp;
            if (interval > getLongTimeRunningSqlIntervalThreshold()) {
                logger.warn(
                        "SQL Statement [{}] with parameter object [{}] ran out of the normal time range, it consumed [{}] milliseconds.",
                        new Object[] { statementName, parameterObject, interval });
            }
        }
    }
}

From source file:org.cloudfoundry.identity.uaa.client.JdbcClientMetadataProvisioning.java

@Override
public ClientMetadata update(ClientMetadata resource) {
    logger.debug("Updating metadata for client: " + resource.getClientId());

    updateClientNameIfNotEmpty(resource);
    int updated = template.update(CLIENT_METADATA_UPDATE, ps -> {
        int pos = 1;
        ps.setBoolean(pos++, resource.isShowOnHomePage());
        URL appLaunchUrl = resource.getAppLaunchUrl();
        ps.setString(pos++, appLaunchUrl == null ? null : appLaunchUrl.toString());
        String appIcon = resource.getAppIcon();
        if (appIcon != null) {
            byte[] decodedAppIcon = Base64Utils.decode(appIcon.getBytes());
            ps.setBinaryStream(pos++, new ByteArrayInputStream(decodedAppIcon), decodedAppIcon.length);
        } else {/*  w ww .j  a v a2 s  . c  om*/
            ps.setBinaryStream(pos++, new ByteArrayInputStream(new byte[] {}), 0);
        }
        ps.setString(pos++, resource.getClientId());
        String zone = IdentityZoneHolder.get().getId();
        ps.setString(pos++, zone);
    });

    ClientMetadata resultingClientMetadata = retrieve(resource.getClientId());

    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }

    return resultingClientMetadata;
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimUserProvisioning.java

@Override
public ScimUserInterface update(final String id, final ScimUserInterface user)
        throws InvalidScimResourceException {

    validate(user);/*from   w  ww .j a v  a 2 s .  com*/
    logger.debug("Updating user " + user.getUserName());

    int updated = jdbcTemplate.update(UPDATE_USER_SQL, new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {

            ps.setInt(1, user.getVersion() + 1);
            ps.setTimestamp(2, new Timestamp(new Date().getTime()));
            ps.setString(3, user.getUserName());
            ps.setString(4, user.getPrimaryEmail());
            ps.setString(5, user.getGivenName());
            ps.setString(6, user.getFamilyName());
            ps.setBoolean(7, user.isActive());
            ps.setString(8, extractPhoneNumber(user));
            ps.setBoolean(9, user.isVerified());
            ps.setString(10, id);
            ps.setInt(11, user.getVersion());
        }
    });
    ScimUserInterface result = retrieve(id);
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d", id,
                        result.getVersion(), user.getVersion()));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    return result;
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimUserProvisioning.java

private ScimUserInterface deactivateUser(ScimUserInterface user, int version) {

    logger.debug("Deactivating user: " + user.getId());
    int updated;//from ww  w  .  j  a  v a2 s. co m
    if (version < 0) {
        // Ignore
        updated = jdbcTemplate.update(DEACTIVATE_USER_SQL, false, user.getId());
    } else {
        updated = jdbcTemplate.update(DEACTIVATE_USER_SQL + " and version=?", false, user.getId(), version);
    }
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d",
                        user.getId(), user.getVersion(), version));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    user.setActive(false);
    return user;
}

From source file:org.cloudfoundry.identity.uaa.scim.dao.standard.JdbcScimUserProvisioning.java

@Override
public ScimUserInterface verifyUser(String id, int version)
        throws ScimResourceNotFoundException, InvalidScimResourceException {

    logger.debug("Verifying user: " + id);
    int updated;//from   w ww .j a v  a2 s. c  om
    if (version < 0) {
        // Ignore
        updated = jdbcTemplate.update(VERIFY_USER_SQL, true, id);
    } else {
        updated = jdbcTemplate.update(VERIFY_USER_SQL + " and version=?", true, id, version);
    }
    ScimUserInterface user = retrieve(id);
    if (updated == 0) {
        throw new OptimisticLockingFailureException(
                String.format("Attempt to update a user (%s) with wrong version: expected=%d but found=%d",
                        user.getId(), user.getVersion(), version));
    }
    if (updated > 1) {
        throw new IncorrectResultSizeDataAccessException(1);
    }
    return user;
}