/** * This method will return the first result from the given query or null if no results were found * * @param query * The query that is to be executed * @return One result from the given query or null if none was found */ public T singleResult(final Query query) { query.setMaxResults(1); List<T> list = list(query); if (CollectionUtils.isNotEmpty(list)) { return list.get(0); } else { return null; } }
/** * Retrieve a unique result from the query. If multiple results CAN be * retrieved an exception will be thrown, * so only use when the criteria state uniqueness in the database. * @param criteriaQuery JPA criteria * @return a DAO specified by the criteria */ public T uniqueResult(Context context, CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset) throws SQLException { List<T> list = list(context, criteriaQuery, cacheable, clazz, maxResults, offset); if (CollectionUtils.isNotEmpty(list)) { if (list.size() == 1) { return list.get(0); } else { throw new IllegalArgumentException("More than one result found"); } } else { return null; } }