use of javax.persistence.NonUniqueResultException in project pic-sure-auth-microapp by hms-dbmi.
the class UserRepository method findOrCreate.
/**
* @return
*/
public User findOrCreate(User inputUser) {
User user = null;
String subject = inputUser.getSubject();
try {
user = findBySubject(subject);
logger.info("findOrCreate(), trying to find user: {subject: " + subject + "}, and found a user with uuid: " + user.getUuid() + ", subject: " + user.getSubject());
} catch (NoResultException e) {
logger.debug("findOrCreate() subject " + subject + " could not be found by `entityManager`, going to create a new user.");
user = createUser(inputUser);
} catch (NonUniqueResultException e) {
logger.error("findOrCreate() " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
return user;
}
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);
}
}
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();
}
}
Aggregations