我需要从DB只带一个结果.我怎么能用JPA做到这一点?
Select top 1 * from table
Run Code Online (Sandbox Code Playgroud)
我试过了
"从表格中选择t"
query.setMaxResults(1);
query.getSingleResult();
Run Code Online (Sandbox Code Playgroud)
但没有奏效.还有其他想法吗?
一个简单的问题,因为我确信这是愚蠢的.我有以下查询,我可以在NetBeans sql命令窗口中执行:
SELECT TOP 25 * FROM ARCUST_BIG WHERE arcustno<='300000' ORDER BY arcustno DESC
Run Code Online (Sandbox Code Playgroud)
我的目标是将它放在我的ArcustRepository类中:
公共接口ArcustRepository扩展了JpaRepository {
Arcust findByPrimaryKey(String id);
@Query("SELECT COUNT(a) FROM Arcust a")
Long countAll();
@Query("SELECT TOP 25 a FROM Arcust a WHERE a.arcustno<='?1' ORDER BY a.arcustno DESC")
List<Arcust> findByTop(String arcustno);
}
Run Code Online (Sandbox Code Playgroud)
但是,findBytop查询似乎不起作用,当我用tomcat7启动我的服务时返回:
2013-08-15 08:15:20 ERROR ContextLoader:319 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'arcustService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.waudware.pics.repository.ArcustRepository com.waudware.pics.service.ArcustService.arcustRepository; nested exception is org.springframework.beans.factory.BeanCreationException: …Run Code Online (Sandbox Code Playgroud) 有没有办法让CrudRepository接口对具有多行的表进行排序并简单地返回第一行,例如按时间戳排序以仅返回最新的行?
public interface ImportReceiptRepository extends CrudRepository<ImportReceipt, Long>
{
ImportReceipt getOneByImportTypeOrderByTimestampDesc(String importType);
ImportReceipt findOneByImportTypeOrderByTimestampDesc(String importType);
}
Run Code Online (Sandbox Code Playgroud)
findOneBy ...和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)
...
Run Code Online (Sandbox Code Playgroud)
或者,换句话说,CrudRepository相当于:
SELECT * FROM ImportReceipts ORDER BY timestamp DESC LIMIT 0,1;
Run Code Online (Sandbox Code Playgroud)