一个简短的问题,因为我确信这是愚蠢的事情。我有以下可以在 NetBeans sql 命令窗口中执行的查询:
SELECT TOP 25 * FROM ARCUST_BIG WHERE arcustno<='300000' ORDER BY arcustno DESC
我的目标是把它放在我的 ArcustRepository 类中:
公共(public)接口(interface) 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);
}
但是,该 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: Error creating bean with name 'arcustRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.waudware.pics.repository.ArcustRepository.findByTop(java.lang.String)!
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.waudware.pics.repository.ArcustRepository.findByTop(java.lang.String)!
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: 25 near line 1, column 12 [SELECT TOP 25 a FROM com.waudware.pics.domain.Arcust a WHERE a.arcustno<='?1' ORDER BY a.arcustno DESC]
最佳答案
# 纯 SQL
使用“限制”
SELECT * FROM ARCUST_BIG
WHERE arcustno<='300000' ORDER BY arcustno DESC Limit 0, 25
注意:JPA 支持使用方法 createNativeQuery() 或使用注解 @NamedNativeQuery 创建 native 查询 JPA Native Query select and cast object 对象
#JPA
List<Arcust> findTop25ByArcustnoLessThanOrderByArcustnoDesc(String arcustno);
关于java - 我将如何在 Spring 数据存储库中编写 SELECT TOP 25 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252582/