Search in sources :

Example 66 with NonUniqueResultException

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;
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) User(edu.harvard.hms.dbmi.avillach.auth.data.entity.User) NoResultException(javax.persistence.NoResultException)

Example 67 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 68 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)

Example 69 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)

Aggregations

NonUniqueResultException (javax.persistence.NonUniqueResultException)69 NoResultException (javax.persistence.NoResultException)57 Query (javax.persistence.Query)30 EntityManager (javax.persistence.EntityManager)11 List (java.util.List)7 PersistenceException (javax.persistence.PersistenceException)7 Collection (java.util.Collection)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 QueryBuilder (org.meveo.commons.utils.QueryBuilder)5 IOException (java.io.IOException)4 Date (java.util.Date)4 EntityExistsException (javax.persistence.EntityExistsException)4 EntityNotFoundException (javax.persistence.EntityNotFoundException)4 OptimisticLockException (javax.persistence.OptimisticLockException)4 TransactionRequiredException (javax.persistence.TransactionRequiredException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 TypedQuery (javax.persistence.TypedQuery)3 WebResource (org.asqatasun.entity.subject.WebResource)3 Test (org.junit.jupiter.api.Test)3