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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With