java CrudRepository:返回一个结果,按列排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28679864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CrudRepository: Return one result, ordered by a column
提问by Christopher
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?
CrudRepository 接口有没有办法对具有多行的表进行排序并简单地返回第一行,例如按时间戳排序以仅返回最新行?
public interface ImportReceiptRepository extends CrudRepository<ImportReceipt, Long>
{
ImportReceipt getOneByImportTypeOrderByTimestampDesc(String importType);
ImportReceipt findOneByImportTypeOrderByTimestampDesc(String importType);
}
Both findOneBy... and getOneBy... throw:
findOneBy... 和 getOneBy... 都抛出:
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:
或者,换句话说,CrudRepository 相当于什么:
SELECT * FROM ImportReceipts ORDER BY timestamp DESC LIMIT 0,1;
回答by Christopher
Bludream's answer from setMaxResults for Spring-Data-JPA annotation?… has the answer.
Bludream对 Spring-Data-JPA 注释的setMaxResults 的回答?……有了答案。
The syntax is "findFirst" or "findTop10" though it still returns a list.
语法是“findFirst”或“findTop10”,但它仍然返回一个列表。
List<ImportReceipt> findFirstByImportTypeOrderByTimestampDesc(String importType);
I suppose List will always be of size() 0 or 1.
我想 List 的 size() 总是 0 或 1。