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;
}
}
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();
}
}
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;
}
}
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;
}
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);
}
}
Aggregations