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(String msg, int expectedSize, int actualSize) 

Source Link

Document

Constructor for IncorrectResultSizeDataAccessException.

Usage

From source file:org.socialsignin.spring.data.dynamodb.query.AbstractMultipleEntityQuery.java

@Override
public T getSingleResult() {
    List<T> results = getResultList();
    if (results.size() > 1) {
        throw new IncorrectResultSizeDataAccessException("result returns more than one elements", 1,
                results.size());// w w  w .  j a  va  2  s .c  o  m
    }
    if (results.size() == 0) {
        throw new EmptyResultDataAccessException("No results found", 1);
    }
    return results.get(0);
}

From source file:org.seasar.doma.boot.autoconfigure.DomaPersistenceExceptionTranslator.java

@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
    if (!(ex instanceof JdbcException)) {
        // Fallback to other translators if not JdbcException
        return null;
    }/* w  w  w .j ava  2  s . c o m*/

    if (ex instanceof OptimisticLockException) {
        return new OptimisticLockingFailureException(ex.getMessage(), ex);
    } else if (ex instanceof UniqueConstraintException) {
        return new DuplicateKeyException(ex.getMessage(), ex);
    } else if (ex instanceof NonUniqueResultException || ex instanceof NonSingleColumnException) {
        return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
    } else if (ex instanceof NoResultException) {
        return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
    } else if (ex instanceof UnknownColumnException || ex instanceof ResultMappingException) {
        return new TypeMismatchDataAccessException(ex.getMessage(), ex);
    }

    if (ex.getCause() instanceof SQLException) {
        SQLException e = (SQLException) ex.getCause();
        String sql = null;
        if (ex instanceof SqlExecutionException) {
            sql = ((SqlExecutionException) ex).getRawSql();
        }
        return translator.translate(ex.getMessage(), sql, e);
    }

    return new UncategorizedDataAccessException(ex.getMessage(), ex) {
    };
}

From source file:de.codesourcery.eve.skills.db.dao.HibernateDAO.java

protected static <T> T getExactlyOneResult(List<T> result) {
    if (result.isEmpty()) {
        throw new EmptyResultDataAccessException(1);
    }/*from  w ww .j av  a2  s  . com*/
    if (result.size() > 1) {
        throw new IncorrectResultSizeDataAccessException("Internal error, got more than I expected?", 1,
                result.size());
    }
    return result.get(0);
}

From source file:de.codesourcery.eve.skills.db.dao.InventoryTypeDAO.java

@Override
public InventoryType getInventoryTypeByName(final String name) {

    InventoryType result = typeByName.get(name);

    if (result != null) {
        return result;
    }//from www .j  a  v  a  2  s. c o m

    result = execute(new HibernateCallback<InventoryType>() {

        @Override
        public InventoryType doInSession(Session session) {
            final Query query = session.createQuery("from InventoryType where name  = :itemName");
            query.setParameter("itemName", name);
            final List<InventoryType> result = query.list();

            if (result.isEmpty() || result.size() > 1) {
                throw new IncorrectResultSizeDataAccessException(
                        "" + "Unexpected results for invType '" + name + "', found " + result.size() + " ?", 1,
                        result.size());
            }
            return result.get(0);
        }
    });
    typeByName.putIfAbsent(name, result);
    return result;
}

From source file:com.laxser.blitz.lama.core.SelectOperation.java

@Override
public Object execute(Map<String, Object> parameters) {
    // /*from  ww  w .  j a  v  a 2 s.c om*/
    List<?> listResult = dataAccess.select(sql, modifier, parameters, rowMapper);
    final int sizeResult = listResult.size();

    //  Result ?
    if (returnType.isAssignableFrom(List.class)) {

        //   List ?
        return listResult;

    } else if (returnType.isArray() && byte[].class != returnType) {
        Object array = Array.newInstance(returnType.getComponentType(), sizeResult);
        if (returnType.getComponentType().isPrimitive()) {
            int len = listResult.size();
            for (int i = 0; i < len; i++) {
                Array.set(array, i, listResult.get(i));
            }
        } else {
            listResult.toArray((Object[]) array);
        }
        return array;

    } else if (Map.class.isAssignableFrom(returnType)) {
        //   KeyValuePair ??  Map 
        // entry.key?nullHashMap
        Map<Object, Object> map;
        if (returnType.isAssignableFrom(HashMap.class)) {

            map = new HashMap<Object, Object>(listResult.size() * 2);

        } else if (returnType.isAssignableFrom(Hashtable.class)) {

            map = new Hashtable<Object, Object>(listResult.size() * 2);

        } else {

            throw new Error(returnType.toString());
        }
        for (Object obj : listResult) {
            if (obj == null) {
                continue;
            }

            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;

            if (map.getClass() == Hashtable.class && entry.getKey() == null) {
                continue;
            }

            map.put(entry.getKey(), entry.getValue());
        }

        return map;

    } else if (returnType.isAssignableFrom(HashSet.class)) {

        //   Set ?
        return new HashSet<Object>(listResult);

    } else {

        if (sizeResult == 1) {
            // ?  Bean?Boolean
            return listResult.get(0);

        } else if (sizeResult == 0) {

            // null
            if (returnType.isPrimitive()) {
                String msg = "Incorrect result size: expected 1, actual " + sizeResult + ": "
                        + modifier.toString();
                throw new EmptyResultDataAccessException(msg, 1);
            } else {
                return null;
            }

        } else {
            // IncorrectResultSizeDataAccessException
            String msg = "Incorrect result size: expected 0 or 1, actual " + sizeResult + ": "
                    + modifier.toString();
            throw new IncorrectResultSizeDataAccessException(msg, 1, sizeResult);
        }
    }
}

From source file:com.sinosoft.one.data.jade.statement.SelectQuerier.java

public Object execute(SQLType sqlType, StatementRuntime runtime) {
    String sql = runtime.getSQL();
    Object[] args = runtime.getArgs();
    DataAccess dataAccess = new DataAccessImpl(em);
    List<?> listResult = null;
    Pageable pageable = null;/*from   w  w  w  .jav a  2  s.  c  om*/
    Sort sort = null;
    boolean isPage = false;
    boolean isSort = false;
    Map<String, Object> paramMap = runtime.getParameters();
    for (String key : paramMap.keySet()) {
        if (paramMap.get(key) instanceof Pageable) {
            pageable = (Pageable) paramMap.get(key);
            isPage = true;
        } else if (paramMap.get(key) instanceof Sort) {
            sort = (Sort) paramMap.get(key);
            isSort = true;
        }
    }
    if (isPage && !isSort) {
        if (returnType == Page.class) {
            String countSql = parseCountSql(sql);
            Page<?> page = dataAccess.selectByPage(pageable, sql, countSql, args, rowMapper);
            return page;
        } else {
            try {
                log.error("The return type[" + returnType + "] must be " + Page.class);
                throw new Exception("The return type [\"+returnType+\"] is invalid");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else if (!isPage && isSort) {
        return dataAccess.selectBySort(sort, sql, args, rowMapper);
    } else if (isPage && isSort) {
        try {
            log.error("Can not use Params:[" + Pageable.class + " and " + Sort.class + "at the same time.");
            throw new Exception(
                    "Can not use Params:[" + Pageable.class + " and " + Sort.class + "at the same time.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {

        listResult = dataAccess.select(sql, args, rowMapper);
        final int sizeResult = listResult.size();

        //  Result ?
        if (returnType.isAssignableFrom(List.class)) {

            //   List ?
            return listResult;

        } else if (returnType.isArray() && byte[].class != returnType) {
            Object array = Array.newInstance(returnType.getComponentType(), sizeResult);
            if (returnType.getComponentType().isPrimitive()) {
                int len = listResult.size();
                for (int i = 0; i < len; i++) {
                    Array.set(array, i, listResult.get(i));
                }
            } else {
                listResult.toArray((Object[]) array);
            }
            return array;

        } else if (Map.class.isAssignableFrom(returnType)) {
            //   KeyValuePair ??  Map 
            // entry.key?nullHashMap
            Map<Object, Object> map;
            if (returnType.isAssignableFrom(HashMap.class)) {

                map = new HashMap<Object, Object>(listResult.size() * 2);

            } else if (returnType.isAssignableFrom(Hashtable.class)) {

                map = new Hashtable<Object, Object>(listResult.size() * 2);

            } else {

                throw new Error(returnType.toString());
            }
            for (Object obj : listResult) {
                if (obj == null) {
                    continue;
                }

                Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;

                if (map.getClass() == Hashtable.class && entry.getKey() == null) {
                    continue;
                }

                map.put(entry.getKey(), entry.getValue());
            }

            return map;

        } else if (returnType.isAssignableFrom(HashSet.class)) {

            //   Set ?
            return new HashSet<Object>(listResult);

        } else {

            if (sizeResult == 1) {
                // ?  Bean?Boolean
                return listResult.get(0);

            } else if (sizeResult == 0) {

                // null
                if (returnType.isPrimitive()) {
                    String msg = "Incorrect result size: expected 1, actual " + sizeResult + ": "
                            + runtime.getMetaData();
                    throw new EmptyResultDataAccessException(msg, 1);
                } else {
                    return null;
                }

            } else {
                // IncorrectResultSizeDataAccessException
                String msg = "Incorrect result size: expected 0 or 1, actual " + sizeResult + ": "
                        + runtime.getMetaData();
                throw new IncorrectResultSizeDataAccessException(msg, 1, sizeResult);
            }
        }
    }
    return listResult;
}

From source file:com.gzj.tulip.jade.statement.SelectQuerier.java

protected ResultConverter makeResultConveter() {
    ResultConverter converter;/* ww  w. ja va2 s  .c om*/
    if (List.class == returnType || Collection.class == returnType || Iterable.class == returnType) {
        converter = new ResultConverter() {

            @Override
            public Object convert(StatementRuntime runtime, List<?> listResult) {
                return listResult;
            }

        };
    } else if (ArrayList.class == returnType) {
        converter = new ResultConverter() {

            @Override
            public Object convert(StatementRuntime runtime, List<?> listResult) {
                return new ArrayList(listResult);
            }

        };
    } else if (LinkedList.class == returnType) {
        converter = new ResultConverter() {

            @Override
            public Object convert(StatementRuntime runtime, List<?> listResult) {
                return new LinkedList(listResult);
            }

        };
    } else if (Set.class == returnType || HashSet.class == returnType) {
        converter = new ResultConverter() {

            @Override
            public Object convert(StatementRuntime runtime, List<?> listResult) {
                return new HashSet(listResult);
            }

        };
    } else if (Collection.class.isAssignableFrom(returnType)) {
        converter = new ResultConverter() {

            @Override
            public Object convert(StatementRuntime runtime, List<?> listResult) {
                Collection listToReturn;
                try {
                    listToReturn = (Collection) returnType.newInstance();
                } catch (Exception e) {
                    throw new Error("error to create instance of " + returnType.getName());
                }
                listToReturn.addAll(listResult);
                return listToReturn;
            }

        };
    } else if (Iterator.class == returnType) {
        converter = new ResultConverter() {

            @Override
            public Object convert(StatementRuntime runtime, List<?> listResult) {
                return listResult.iterator();
            }

        };
    } else if (returnType.isArray() && byte[].class != returnType) {
        if (returnType.getComponentType().isPrimitive()) {
            converter = new ResultConverter() {

                @Override
                public Object convert(StatementRuntime runtime, List<?> listResult) {
                    Object array = Array.newInstance(returnType.getComponentType(), listResult.size());
                    int len = listResult.size();
                    for (int i = 0; i < len; i++) {
                        Array.set(array, i, listResult.get(i));
                    }
                    return array;
                }

            };
        } else {
            converter = new ResultConverter() {

                @Override
                public Object convert(StatementRuntime runtime, List<?> listResult) {
                    Object array = Array.newInstance(returnType.getComponentType(), listResult.size());
                    return listResult.toArray((Object[]) array);
                }

            };
        }

    } else if (Map.class == returnType || HashMap.class == returnType) {
        converter = new MapConverter() {
            @Override
            protected Map creatMap(StatementRuntime runtime) {
                return new HashMap();
            }
        };
    } else if (Hashtable.class == returnType) {
        converter = new MapConverter() {
            @Override
            protected Map creatMap(StatementRuntime runtime) {
                return new Hashtable();
            }
        };
    } else if (Map.class.isAssignableFrom(returnType)) {
        converter = new MapConverter() {
            @Override
            protected Map creatMap(StatementRuntime runtime) {
                try {
                    return (Map) returnType.newInstance();
                } catch (Exception e) {
                    throw new Error("error to create instance of " + returnType.getName());
                }
            }
        };
    }
    // 
    else {
        converter = new ResultConverter() {

            @Override
            public Object convert(StatementRuntime runtime, List<?> listResult) {
                final int sizeResult = listResult.size();
                if (sizeResult == 1) {
                    // ?  Bean?Boolean
                    return listResult.get(0);

                } else if (sizeResult == 0) {

                    // null
                    if (returnType.isPrimitive()) {
                        String msg = "Incorrect result size: expected 1, actual " + sizeResult + ": "
                                + runtime.getMetaData();
                        throw new EmptyResultDataAccessException(msg, 1);
                    } else {
                        return null;
                    }

                } else {
                    // IncorrectResultSizeDataAccessException
                    String msg = "Incorrect result size: expected 0 or 1, actual " + sizeResult + ": "
                            + runtime.getMetaData();
                    throw new IncorrectResultSizeDataAccessException(msg, 1, sizeResult);
                }
            }
        };
    }
    return converter;
}

From source file:de.codesourcery.eve.skills.db.dao.BlueprintTypeDAO.java

@Override
public Blueprint getBlueprintByName(final String name) {
    return execute(new HibernateCallback<Blueprint>() {

        @SuppressWarnings("unchecked")
        @Override//from ww w. ja v a  2s  .  c o m
        public Blueprint doInSession(Session session) {
            final Query query = session.createQuery(" select b from " + "BlueprintType b , InventoryType t "
                    + " where b.blueprintType = t.typeId and t.name = :blueprintName");

            query.setParameter("blueprintName", name);

            final List<BlueprintType> types = (List<BlueprintType>) query.list();

            if (types.size() == 1) {
                return createBlueprint(types.get(0));
            }

            throw new IncorrectResultSizeDataAccessException("Expected one blueprint with name '" + name + "'",
                    1, types.size());
        }
    });
}

From source file:de.codesourcery.eve.skills.db.dao.BlueprintTypeDAO.java

protected Blueprint getTech1Variation(final BlueprintType tech2Blueprint) throws NoTech1VariantException {

    if (tech2Blueprint.getTechLevel() != 2) {
        throw new IllegalArgumentException("This method requires a Tech2 blueprint");
    }/*from   w  w  w .j av a  2  s.  co m*/
    /*
     * [...] you take the invBlueprintTypes.productTypeID field for the 
     * tech 1 blueprint and look it up in invMetaTypes.parentTypeID 
     * with invMetaTypes.metaGroup='2' 
     * that will give you the tech 2 items that can be invented.       
     */

    return execute(new HibernateCallback<Blueprint>() {

        @SuppressWarnings("unchecked")
        @Override
        public Blueprint doInSession(Session session) {

            final Query query = session.createSQLQuery(
                    "select b.* from " + "invBlueprintTypes b , invMetaTypes g " + " where g.typeID = "
                            + tech2Blueprint.getProductType().getId() + " and b.productTypeID = g.parentTypeID")
                    .addEntity(BlueprintType.class);

            final List<BlueprintType> types = (List<BlueprintType>) query.list();

            if (types.isEmpty()) {
                throw new NoTech1VariantException(tech2Blueprint);
            } else if (types.size() > 1) {
                throw new IncorrectResultSizeDataAccessException(
                        "Unable to find Tech1 variant of " + tech2Blueprint.getBlueprintType(), 1,
                        types.size());
            }
            return createBlueprint(types.get(0));
        }
    });
}

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

protected E findOneByGsi(String gsiName, QuerySpec spec) {
    Chunk<E> chunk = getFromGSI(gsiName, spec, true /*isUnique*/);
    return Optional
            .ofNullable(chunk.getContent().isEmpty() ? null : Iterables.getOnlyElement(chunk.getContent()))
            .orElseThrow(() -> new IncorrectResultSizeDataAccessException(
                    "could not find one matching record for spec:" + spec.toString(), 1 /*expected*/,
                    0 /*actual*/));
}