Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CrudRepository: Return one result, ordered by a column [duplicate]

Is there a way for a CrudRepository interface to sort a table with multiple rows and simply return the first row, e.g. sort by a timestamp to return only the latest row?

public interface ImportReceiptRepository extends CrudRepository<ImportReceipt, Long>
{
    ImportReceipt getOneByImportTypeOrderByTimestampDesc(String importType);
    ImportReceipt findOneByImportTypeOrderByTimestampDesc(String importType);
}

Both findOneBy... and getOneBy... throw:

org.springframework.dao.IncorrectResultSizeDataAccessException: result returns more than one elements; nested exception is javax.persistence.NonUniqueResultException: result returns more than one elements
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:395)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:216)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy109.findOneByImportTypeOrderByTimestampDesc(Unknown Source)
at edu.ucdavis.dss.dw.services.DefaultImportReceiptService.getLatestOneByImportType(DefaultImportReceiptService.java:26)
...

Or, putting it another way, what's the CrudRepository equivalent of:

SELECT * FROM ImportReceipts ORDER BY timestamp DESC LIMIT 0,1;
like image 279
Christopher Avatar asked Feb 23 '15 17:02

Christopher


1 Answers

Bludream's answer from setMaxResults for Spring-Data-JPA annotation?… has the answer.

The syntax is "findFirst" or "findTop10" though it still returns a list.

List<ImportReceipt> findFirstByImportTypeOrderByTimestampDesc(String importType);

I suppose List will always be of size() 0 or 1.

like image 130
Christopher Avatar answered Sep 30 '22 13:09

Christopher