休眠本机查询将引发“ ORDER BY FIELD()”的异常

时间:2020-03-18 10:21:39

标签: mysql spring hibernate spring-boot spring-data-jpa

我有一个Spring Boot项目(版本:2.0.0.M1),我正在使用MySQL DB。

我的用户存储库扩展了JpaRepository<UserEntity, Integer>。将Spring Boot版本升级到2.x.x.RELEASE后,以下方法将引发异常:

@Query(value = "Select * from user as u where u.is_deleted = FALSE order by field (u.id, :idList)",
nativeQuery = true)
Page<UserEntity> findNonDeletedOrderByIdList(@Param("idList") List<Integer> idList, Pageable pageable);

例外是:

java.lang.IllegalArgumentException:无法将给定参数名称[idList]解析为QueryParameter引用

请帮助我。

注意:它可能与Hibernate criteria for sql ORDER BY FIELD重复

更新:

我不知道为什么,在AND u.id IN (:idList)之前插入order by后有效

@Query(value = "Select * from user as u where u.is_deleted = FALSE AND u.id IN (:idList) order by field (u.id, :idList)", nativeQuery = true)
Page<UserEntity> findNonDeletedOrderByIdList(@Param("idList") List<Integer> idList, Pageable pageable);

有人可以解释吗?

2 个答案:

答案 0 :(得分:0)

如果我了解您的问题,我认为您的SQL无效。

订购依据不能有参数

尝试一下:

@Query(value = "Select * from user as u where u.is_deleted = FALSE order by u.id ASC, nativeQuery = true)
Page<UserEntity> findNonDeletedOrderByIdList(Pageable pageable);

答案 1 :(得分:0)

我不知道为什么,在AND u.id IN (:idList)之前插入order by后有效

@Query(value = "Select * from user as u where u.is_deleted = FALSE AND u.id IN (:idList) order by field (u.id, :idList)", nativeQuery = true)
Page<UserEntity> findNonDeletedOrderByIdList(@Param("idList") List<Integer> idList, Pageable pageable);