Search in sources :

Example 61 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project SORMAS-Project by hzi-braunschweig.

the class PopulationDataFacadeEjb method getProjectedDistrictPopulation.

@Override
public Integer getProjectedDistrictPopulation(String districtUuid) {
    Float growthRate = districtService.getByUuid(districtUuid).getGrowthRate();
    if (growthRate == null || growthRate == 0) {
        return getDistrictPopulation(districtUuid);
    }
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<PopulationData> cq = cb.createQuery(PopulationData.class);
    Root<PopulationData> root = cq.from(PopulationData.class);
    PopulationDataCriteria criteria = new PopulationDataCriteria().ageGroupIsNull(true).sexIsNull(true).district(new DistrictReferenceDto(districtUuid, null, null));
    Predicate filter = service.buildCriteriaFilter(criteria, cb, root);
    cq.where(filter);
    try {
        PopulationData populationData = em.createQuery(cq).getSingleResult();
        return InfrastructureHelper.getProjectedPopulation(populationData.getPopulation(), populationData.getCollectionDate(), growthRate);
    } catch (NoResultException | NonUniqueResultException e) {
        return null;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) NonUniqueResultException(javax.persistence.NonUniqueResultException) PopulationDataCriteria(de.symeda.sormas.api.infrastructure.PopulationDataCriteria) NoResultException(javax.persistence.NoResultException) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) Predicate(javax.persistence.criteria.Predicate)

Example 62 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project so by onap.

the class InfraActiveRequestsRepositoryImpl method getSingleResult.

protected <T> T getSingleResult(final Query query) {
    query.setMaxResults(1);
    final List<T> list = query.getResultList();
    if (list == null || list.isEmpty()) {
        return null;
    } else if (list.size() == 1) {
        return list.get(0);
    } else {
        throw new NonUniqueResultException();
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException)

Example 63 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project projectforge by micromata.

the class TeamEventDao method getByUid.

public TeamEventDO getByUid(Integer calendarId, final String uid, final boolean excludeDeleted) {
    if (uid == null) {
        return null;
    }
    final StringBuilder sqlQuery = new StringBuilder();
    final List<Object> params = new ArrayList<>();
    sqlQuery.append("select e from TeamEventDO e where e.uid = :uid");
    params.add("uid");
    params.add(uid);
    if (excludeDeleted) {
        sqlQuery.append(" AND e.deleted = :deleted");
        params.add("deleted");
        params.add(false);
    }
    // workaround to still handle old requests
    if (calendarId != null) {
        sqlQuery.append(" AND e.calendar.id = :calendarId");
        params.add("calendarId");
        params.add(calendarId);
    }
    try {
        return emgrFac.runRoTrans(emgr -> emgr.selectSingleAttached(TeamEventDO.class, sqlQuery.toString(), params.toArray()));
    } catch (NoResultException | NonUniqueResultException e) {
        return null;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) TeamEventDO(org.projectforge.business.teamcal.event.model.TeamEventDO) NoResultException(javax.persistence.NoResultException)

Example 64 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project meveo by meveo-org.

the class SecuredBusinessEntityService method getEntityByCode.

public BusinessEntity getEntityByCode(String entityClassName, String code) {
    try {
        Class<?> businessEntityClass = Class.forName(ReflectionUtils.getCleanClassName(entityClassName));
        QueryBuilder qb = new QueryBuilder(businessEntityClass, "e", null);
        qb.addCriterion("e.code", "=", code, true);
        return (BusinessEntity) qb.getQuery(getEntityManager()).getSingleResult();
    } catch (NoResultException e) {
        log.debug("No {} of code {} found", getEntityClass().getSimpleName(), code, e);
    } catch (NonUniqueResultException e) {
        log.error("More than one entity of type {} with code {} found", entityClass, code, e);
    } catch (ClassNotFoundException e) {
        log.error("Unable to create entity class for query", e);
    }
    return null;
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) QueryBuilder(org.meveo.commons.utils.QueryBuilder) NoResultException(javax.persistence.NoResultException) BusinessEntity(org.meveo.model.BusinessEntity)

Example 65 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project meveo by meveo-org.

the class BusinessService method findByCode.

/**
 * Find entity by code - strict match.
 *
 * @param code Code to match
 * @param fetchFields Fields to fetch
 * @param additionalSql Additional sql to append to the find clause
 * @param additionalParameters An array of Parameter names and values for additional sql
 * @return A single entity matching code
 */
@SuppressWarnings("unchecked")
protected P findByCode(String code, List<String> fetchFields, String additionalSql, Object... additionalParameters) {
    if (StringUtils.isBlank(code)) {
        return null;
    }
    QueryBuilder qb = new QueryBuilder(getEntityClass(), "be", fetchFields);
    qb.addCriterion("be.code", "=", code, true);
    if (additionalSql != null) {
        qb.addSqlCriterionMultiple(additionalSql, additionalParameters);
    }
    try {
        return (P) qb.getQuery(getEntityManager()).getSingleResult();
    } catch (NoResultException e) {
        log.debug("No {} of code {} found", getEntityClass().getSimpleName(), code);
        return null;
    } catch (NonUniqueResultException e) {
        log.error("More than one entity of type {} with code {} found. A first entry is returned.", entityClass, code);
        return (P) qb.getQuery(getEntityManager()).getResultList().get(0);
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) QueryBuilder(org.meveo.commons.utils.QueryBuilder) NoResultException(javax.persistence.NoResultException)

Aggregations

NonUniqueResultException (javax.persistence.NonUniqueResultException)65 NoResultException (javax.persistence.NoResultException)54 Query (javax.persistence.Query)29 EntityManager (javax.persistence.EntityManager)11 List (java.util.List)7 PersistenceException (javax.persistence.PersistenceException)6 Collection (java.util.Collection)5 QueryBuilder (org.meveo.commons.utils.QueryBuilder)5 IOException (java.io.IOException)4 Date (java.util.Date)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 EntityExistsException (javax.persistence.EntityExistsException)3 EntityNotFoundException (javax.persistence.EntityNotFoundException)3 OptimisticLockException (javax.persistence.OptimisticLockException)3 TransactionRequiredException (javax.persistence.TransactionRequiredException)3 TypedQuery (javax.persistence.TypedQuery)3 WebResource (org.asqatasun.entity.subject.WebResource)3 HibernateException (org.hibernate.HibernateException)3