记录JPA报错

First Post:

Last Update:

w

出现场景

1
2
3
4
5
6
7
8
9
10
11
12
 Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist

Caused by: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:502)
at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:692)
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:181)
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)

原因:在使用hibernate做连接查询中,映射实体VO类时,出现此错误,,where条件后面加入ORDER BY ?#{?pageable},就完美解决了这个问题;

应该使用原生SQL语法添加WHERE条件

最终改成:

改为 : PI.id= ?1 …就可以了

SpringCloud JAP之@Query如何实现分页功能

完成:第一遍

1.SpringCloud JAP之@Query如何实现分页功能?

步骤一:在UserRepository中添加给AgeGroup分页的方法

1
2
3
4
@Query(value = "select * from userspringdata where age_group = ?1",
countQuery = "select count(*) from userspringdata where age_group = ?1",
nativeQuery = true)
public List<UserEntity> selectUsersByAgeGroupPage(String ageGroup, Pageable pageable);

步骤二:在UserController写调用方法

1
2
3
4
5
6
7
8
9
10
11
//@Query and native and pageable    
@GetMapping("findUsersByAgeGroupPage")
public Object getUsersByAgeGroupPage(@RequestParam("ageGroup") String ageGroup,
@RequestParam("currentPage") int currentPage,
@RequestParam("pageSize") int pageSize){

Pageable pageable = PageRequest.of(currentPage, pageSize);

return userRepository.selectUsersByAgeGroupPage(ageGroup, pageable);

}

重启启动类
localhost:8001/user/findUsersByAgeGroupPage?ageGroup=0&currentPage=0&pageSize=2

打赏
支付宝 | Alipay
微信 | WeChat