public Object getSingleResult() throws AuditException, NonUniqueResultException, NoResultException { List result = list(); if ( result == null || result.size() == 0 ) { throw new NoResultException(); } if ( result.size() > 1 ) { throw new NonUniqueResultException(); } return result.get( 0 ); }
@Override public R getSingleResult() { final List<R> resultList = getResultList(); if ( resultList == null || resultList.isEmpty() ) { throw new NoResultException( String.format( "Call to stored procedure [%s] returned no results", getProcedureName() ) ); } else if ( resultList.size() > 1 ) { throw new NonUniqueResultException( String.format( "Call to stored procedure [%s] returned multiple results", getProcedureName() ) ); } return resultList.get( 0 ); }
@Override public R getSingleResult() { try { final List<R> list = list(); if ( list.size() == 0 ) { throw new NoResultException( "No entity found for query" ); } return uniqueElement( list ); } catch ( HibernateException e ) { if ( getProducer().getFactory().getSessionFactoryOptions().isJpaBootstrap() ) { throw getExceptionConverter().convert( e ); } else { throw e; } } }
@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()); }
/** * On validate single result. * * @param results * the results */ protected void onValidateSingleResult(List results) { if (results == null || results.isEmpty()) { log.error("No result found for {} ", kunderaQuery.getJPAQuery()); throw new NoResultException("No result found!"); } if (results.size() > 1) { log.error("Non unique results found for query {} ", kunderaQuery.getJPAQuery()); throw new NonUniqueResultException("Containing more than one result!"); } }
@Test(expected = NoResultException.class) void testFetchAllNoResultsReturned() throws Exception { List<Name> namesLocal = null; when(Service.fetchAllNames(id)).thenThrow( new NoResultException("")) namesLocal = (service.fetchAllNames(id) .... }
List<Article> articles; try{ TypedQuery<Article> query = em.createNamedQuery("Article.findByTitle", Article.class); query.setParameter("articleTitle",title); articles = query.getResultList(); } catch (NoResultException e) { throw new NoResultException(); }
@Override public boolean isLocked(String orcid) { TypedQuery<Boolean> query = entityManager.createQuery("select recordLocked from ProfileEntity where orcid = :orcid", Boolean.class); query.setParameter("orcid", orcid); Boolean result; try { result = query.getSingleResult(); } catch (NoResultException nre) { throw new NoResultException("ORCID iD " + orcid + " not found"); } return result; }
public <T> T last() { final List<T> list = list(); if (list.isEmpty()) { throw new NoResultException(); } return list.get(list.size() - 1); }
public <T> T first() { final List<T> list = list(); if (list.isEmpty()) { throw new NoResultException(); } return list.get(0); }
private ResourceEditProperty findByName(String propertyName, List<ResourceEditProperty> resourceEditProperties) { for (ResourceEditProperty property : resourceEditProperties) { if (property.getTechnicalKey().equals(propertyName)) { return property; } } log.info("Property " + propertyName + " is not in propertylist"); throw new NoResultException("Could not find property " + propertyName); }
@Transactional(readOnly = true) public E findUnique(E entity, SearchParameters sp) { E result = findUniqueOrNone(entity, sp); if (result != null) { return result; } throw new NoResultException("Developper: You expected 1 result but found none !"); }
@Transactional(readOnly = true) public E findUnique(E entity, SearchParameters sp) { E result = findUniqueOrNone(entity, sp); if (result != null) { return result; } throw new NoResultException("Developper: You expected 1 result but found none !"); }
@Override public X getSingleResult() { List<X> resultList = getResultList(); if (resultList.isEmpty()) { throw new NoResultException(); } else if (resultList.size() > 1) { throw new NonUniqueResultException("Query produced " + resultList.size() + " results (expected 1)"); } return resultList.get(0); }
public Object getSingleResult() throws AuditException, NonUniqueResultException, NoResultException { List result = list(); if ( result == null || result.size() == 0 ) { throw new NoResultException(); } if ( result.size() > 1 ) { throw new NonUniqueResultException(); } return result.get( 0 ); }
@Override public X getSingleResult() { List<X> list = getResultList(); switch (list.size()) { case 0: throw new NoResultException("No results for query: " + delegate); case 1: return list.get(0); default: throw new NonUniqueResultException("Expected a single result for query: " + delegate); } }
public String getResourceName(int resourceId) { ResourceEntity resourceEntity = resourceRepository.find(resourceId); if (resourceEntity == null) { throw new NoResultException("No Resource with id " + resourceId); } return resourceEntity.getName(); }
@Override public UID getObjectEntityUID(String ticketUID) throws NoResultException { QJPATicketEntity ticket = QJPATicketEntity.jPATicketEntity; JPAQuery query = new JPAQuery(this.getEntityManager()); TicketEntity ticketEntity = query.from(ticket).where(ticket.uid.eq(ticketUID)).uniqueResult(ticket); if (ticketEntity == null) { throw new NoResultException(); } return ticketEntity.getObjectUID(); } }
@Test public void testFilteredJPAExceptions() { assertFalse(predicate.apply(new EntityExistsException())); assertFalse(predicate.apply(new EntityNotFoundException())); assertFalse(predicate.apply(new LockTimeoutException())); assertFalse(predicate.apply(new NoResultException())); assertFalse(predicate.apply(new NonUniqueResultException())); assertFalse(predicate.apply(new OptimisticLockException())); assertFalse(predicate.apply(new PessimisticLockException())); assertFalse(predicate.apply(new QueryTimeoutException())); assertFalse(predicate.apply(new TransactionRequiredException())); }
@Test public void testNestedFilteredJPAExceptions() { assertFalse(predicate.apply(wrapCause(new EntityExistsException()))); assertFalse(predicate.apply(wrapCause(new EntityNotFoundException()))); assertFalse(predicate.apply(wrapCause(new LockTimeoutException()))); assertFalse(predicate.apply(wrapCause(new NoResultException()))); assertFalse(predicate.apply(wrapCause(new NonUniqueResultException()))); assertFalse(predicate.apply(wrapCause(new OptimisticLockException()))); assertFalse(predicate.apply(wrapCause(new PessimisticLockException()))); assertFalse(predicate.apply(wrapCause(new QueryTimeoutException()))); assertFalse(predicate.apply(wrapCause(new TransactionRequiredException()))); }