Met*_*ing 8 java sql spring-data
一个简单的问题,因为我确信这是愚蠢的.我有以下查询,我可以在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: 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]
Run Code Online (Sandbox Code Playgroud)
Zam*_*mir 15
我会说你需要
List<Arcust> findTop25ByArcustnoLessThanOrderByArcustnoDesc(String arcustno);
Run Code Online (Sandbox Code Playgroud)
这将使用JPA并可能适用于所有数据库,将启动SPRING JPA 1.7.0(Evans发布列车)
我实现CrudRepository而不是JpaRepository
Rak*_*oni 12
纯SQL
使用"限制"
SELECT * FROM ARCUST_BIG
WHERE arcustno<='300000' ORDER BY arcustno DESC Limit 0, 25
Run Code Online (Sandbox Code Playgroud)
JPA
List<Arcust> findTop25ByArcustnoLessThanOrderByArcustnoDesc(String arcustno);
Run Code Online (Sandbox Code Playgroud)
我不确定拉克什的答案是否正确.他似乎在编写SQL,而不是JPA查询语法.
我尝试在JPA @Query中使用LIMIT并得到一个例外,说"限制"无法识别.
@Query("select d from Device d where d.deviceName like CONCAT('%', :deviceName, '%') and d.deviceId not in :notList ORDER BY deviceName DESC Limit 1001")
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: Limit near line 1, column 162
Run Code Online (Sandbox Code Playgroud)
另外,请参阅Hari Shankar的回答,其中说JPA不支持"限制": JPA不支持"限制"
小智 6
您可以在 Repository 中使用您的查询,无需 TOP 25:
@Query("SELECT a FROM Arcust a WHERE a.arcustno<='?1' ORDER BY a.arcustno DESC")
List<Arcust> findByTop(String arcustno, Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
在 Service 中,使用 PageRequest,返回一个 Page 对象:
Page<Arcust> arcusts = arcustRepository.findByTop(arcustno, PageRequest.of(0, 25));
List<Arcust> arcust = arcusts.getContent();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41949 次 |
| 最近记录: |