Search in sources :

Example 6 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project Asqatasun by Asqatasun.

the class OptionElementDAOImpl method findOptionElementFromValueAndOption.

/**
     * 
     * @param value
     * @param option
     * @return 
     */
@Override
public OptionElement findOptionElementFromValueAndOption(String value, Option option) {
    Query query = entityManager.createQuery("SELECT oe FROM " + getEntityClass().getName() + " oe" + " WHERE oe.value=:value" + " AND oe.option=:option");
    query.setParameter("value", value);
    query.setParameter("option", option);
    try {
        return (OptionElement) query.getSingleResult();
    } catch (NoResultException | NonUniqueResultException nre) {
        return null;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) OptionElement(org.asqatasun.webapp.entity.option.OptionElement) NoResultException(javax.persistence.NoResultException)

Example 7 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project hibernate-orm by hibernate.

the class ExceptionConverterImpl method convert.

@Override
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
    Throwable cause = e;
    if (cause instanceof StaleStateException) {
        final PersistenceException converted = wrapStaleStateException((StaleStateException) cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof LockingStrategyException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.exception.LockTimeoutException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.PessimisticLockException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.QueryTimeoutException) {
        final QueryTimeoutException converted = new QueryTimeoutException(cause.getMessage(), cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof ObjectNotFoundException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueObjectException) {
        final EntityExistsException converted = new EntityExistsException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueResultException) {
        final NonUniqueResultException converted = new NonUniqueResultException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof UnresolvableObjectException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof QueryException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof MultipleBagFetchException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof TransientObjectException) {
        try {
            sharedSessionContract.markForRollbackOnly();
        } catch (Exception ne) {
            //we do not want the subsequent exception to swallow the original one
            log.unableToMarkForRollbackOnTransientObjectException(ne);
        }
        //Spec 3.2.3 Synchronization rules
        return new IllegalStateException(e);
    } else {
        final PersistenceException converted = new PersistenceException(cause);
        handlePersistenceException(converted);
        return converted;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransientObjectException(org.hibernate.TransientObjectException) HibernateException(org.hibernate.HibernateException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) NoResultException(javax.persistence.NoResultException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) SQLException(java.sql.SQLException) NonUniqueResultException(javax.persistence.NonUniqueResultException) JDBCException(org.hibernate.JDBCException) EntityNotFoundException(javax.persistence.EntityNotFoundException) StaleStateException(org.hibernate.StaleStateException) OptimisticEntityLockException(org.hibernate.dialect.lock.OptimisticEntityLockException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityExistsException(javax.persistence.EntityExistsException) OptimisticLockException(javax.persistence.OptimisticLockException) StaleObjectStateException(org.hibernate.StaleObjectStateException) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticEntityLockException(org.hibernate.dialect.lock.PessimisticEntityLockException) TransientObjectException(org.hibernate.TransientObjectException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) QueryTimeoutException(javax.persistence.QueryTimeoutException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) QueryTimeoutException(javax.persistence.QueryTimeoutException) QueryException(org.hibernate.QueryException) StaleStateException(org.hibernate.StaleStateException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) PersistenceException(javax.persistence.PersistenceException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException)

Example 8 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project spring-framework by spring-projects.

the class EntityManagerFactoryUtilsTests method testConvertJpaPersistenceException.

/*
	 * Test method for
	 * 'org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessException(PersistenceException)'
	 */
@Test
@SuppressWarnings("serial")
public void testConvertJpaPersistenceException() {
    EntityNotFoundException entityNotFound = new EntityNotFoundException();
    assertSame(JpaObjectRetrievalFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityNotFound).getClass());
    NoResultException noResult = new NoResultException();
    assertSame(EmptyResultDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(noResult).getClass());
    NonUniqueResultException nonUniqueResult = new NonUniqueResultException();
    assertSame(IncorrectResultSizeDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(nonUniqueResult).getClass());
    OptimisticLockException optimisticLock = new OptimisticLockException();
    assertSame(JpaOptimisticLockingFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(optimisticLock).getClass());
    EntityExistsException entityExists = new EntityExistsException("foo");
    assertSame(DataIntegrityViolationException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityExists).getClass());
    TransactionRequiredException transactionRequired = new TransactionRequiredException("foo");
    assertSame(InvalidDataAccessApiUsageException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(transactionRequired).getClass());
    PersistenceException unknown = new PersistenceException() {
    };
    assertSame(JpaSystemException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(unknown).getClass());
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) EntityNotFoundException(javax.persistence.EntityNotFoundException) NoResultException(javax.persistence.NoResultException) EntityExistsException(javax.persistence.EntityExistsException) Test(org.junit.Test)

Example 9 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project ORCID-Source by ORCID.

the class OrcidPropsDaoImpl method getValue.

@Override
public String getValue(String key) {
    Assert.hasText(key, "Cannot look for empty keys");
    Query query = entityManager.createQuery("SELECT value FROM OrcidPropsEntity WHERE key=:key");
    query.setParameter("key", key);
    try {
        return (String) query.getSingleResult();
    } catch (NonUniqueResultException nure) {
        throw nure;
    } catch (NoResultException nre) {
        return null;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException)

Example 10 with NonUniqueResultException

use of javax.persistence.NonUniqueResultException in project ORCID-Source by ORCID.

the class OrcidPropsDaoImpl method exists.

/**
     * Checks if the given key exists in the OrcidPropsEntity table.
     * 
     * @param key
     * @return true if the key exists on the OrcidPropsEntity table
     * @throws NonUniqueResultException
     *             if there are more than one row with the same key name
     * */
@Override
public boolean exists(String key) throws NonUniqueResultException {
    Assert.hasText(key, "Cannot look for empty keys");
    Query query = entityManager.createQuery("FROM OrcidPropsEntity WHERE key=:key");
    query.setParameter("key", key);
    try {
        query.getSingleResult();
    } catch (NoResultException nre) {
        return false;
    } catch (NonUniqueResultException nure) {
        throw nure;
    }
    return true;
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException)

Aggregations

NoResultException (javax.persistence.NoResultException)16 NonUniqueResultException (javax.persistence.NonUniqueResultException)16 Query (javax.persistence.Query)14 List (java.util.List)3 WebResource (org.asqatasun.entity.subject.WebResource)3 Collection (java.util.Collection)2 EntityExistsException (javax.persistence.EntityExistsException)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 PersistenceException (javax.persistence.PersistenceException)2 SQLException (java.sql.SQLException)1 LockTimeoutException (javax.persistence.LockTimeoutException)1 PessimisticLockException (javax.persistence.PessimisticLockException)1 QueryTimeoutException (javax.persistence.QueryTimeoutException)1 RollbackException (javax.persistence.RollbackException)1 TransactionRequiredException (javax.persistence.TransactionRequiredException)1 Parameter (org.asqatasun.entity.parameterization.Parameter)1 ParameterElement (org.asqatasun.entity.parameterization.ParameterElement)1 ParameterFamily (org.asqatasun.entity.parameterization.ParameterFamily)1 PageImpl (org.asqatasun.entity.subject.PageImpl)1