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