首页 > 解决方案 > org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 无效的参数索引 2

问题描述

我正在将我的应用程序升级到 java 8、hibernate5 和 spring 4。有一些现有的 JPQL 查询,其中我使用的 Pageable 在更新后无法正常工作。我低于异常

2019-09-03 07:55:18,814 [http-nio-8080-exec-1] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 07009
2019-09-03 07:55:18,815 [http-nio-8080-exec-1] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Invalid parameter index 2.

我试图将返回类型从 List 更改为 Page 但没有运气

My POM.xml
<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
    <version>5.2.10.Final</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
         <version>1.4.3.RELEASE</version>
</dependency>

Repository 
@Query("select u from UserPasswordHistory as u where u.userId = ?1 order by createDate desc")   
    public List<UserPasswordHistory> findUserPasswordHistroy(Long userId, Pageable page);

Service
public List<UserPasswordHistory> findUserPasswordHistroy(Long userId, int resultLimit){
        try{
            Pageable topResult = new PageRequest(0,resultLimit);
            return pwdHistriyRepository.findUserPasswordHistroy(userId,topResult);

Entity
@Entity
@Table(name="user_password_history")
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE)
public class UserPasswordHistory implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "seq_id")
    private Long id;

    /**
     */
    @Column(name = "user_id")
    private Long userId;

    @Column(name = "password")
    private String password;

我需要做分页

标签: hibernatejpapageable

解决方案


您可以使用 Spring 数据存储库魔术方法,此处无需使用@Query。请试试 :

public interface UserPasswordHistoryRepository extends JpaRepository<UserPasswordHistory, Long> {
    List<UserPasswordHistory> findByUserIdOrderByCreateDateDesc(Long userId, Pageable pageable);
}

例如测试:

userPasswordHistoryRepository.findByUserIdOrderByCreateDateDesc(1L, PageRequest.of(0, 2));

推荐阅读