Spring 具有可分页 SQL 语法错误的数据本机查询
Spring data native query with pageable SQL grammar error
我正在尝试将 spring 数据与本机 SQL 一起使用,并可在 spring-data 2.5.0
和 PostgreSQL 数据库上分页。
@Query(value = "SELECT dense_rank() OVER (ORDER BY rank DESC)," +
"t.id, " +
"FROM table t WHERE t.x > 0 ",
nativeQuery = true)
Page<Object[]> getT(Pageable pageable);
但是我遇到了 SQL异常。结果SQL是
SELECT dense_rank() OVER (ORDER BY rank DESC),t.id FROM table t WHERE t.x > 0 , t.y desc limit 20
在它清晰可见的错误之处,spring-数据不会将 ORDER BY 子句添加到 SQL,可能是因为存在 dense_rank() 的 ORDER BY 子句。如果我删除那个密集的选择,一切正常。
是否有可能使用 spring-data 实现此用例?也许一些可能的解决方法?我想使用整个 Pageable 对象(因此页面大小、偏移和排序)
经过长时间的尝试和寻找答案,我找到了解决方案,更可能是解决方法。 在查询末尾添加 ORDER BY 1 将导致 spring-data 将 , order_column ASC/DESC LIMIT X OFFSET Y
放在 ORDER BY 1
之后(不会修改无论如何结果)。
所以结果应该是
@Query(value = "SELECT dense_rank() OVER (ORDER BY rank DESC)," +
"t.id, " +
"FROM table t WHERE t.x > 0 ORDER BY 1",
nativeQuery = true)
Page<Object[]> getT(Pageable pageable);
我正在尝试将 spring 数据与本机 SQL 一起使用,并可在 spring-data 2.5.0
和 PostgreSQL 数据库上分页。
@Query(value = "SELECT dense_rank() OVER (ORDER BY rank DESC)," +
"t.id, " +
"FROM table t WHERE t.x > 0 ",
nativeQuery = true)
Page<Object[]> getT(Pageable pageable);
但是我遇到了 SQL异常。结果SQL是
SELECT dense_rank() OVER (ORDER BY rank DESC),t.id FROM table t WHERE t.x > 0 , t.y desc limit 20
在它清晰可见的错误之处,spring-数据不会将 ORDER BY 子句添加到 SQL,可能是因为存在 dense_rank() 的 ORDER BY 子句。如果我删除那个密集的选择,一切正常。
是否有可能使用 spring-data 实现此用例?也许一些可能的解决方法?我想使用整个 Pageable 对象(因此页面大小、偏移和排序)
经过长时间的尝试和寻找答案,我找到了解决方案,更可能是解决方法。 在查询末尾添加 ORDER BY 1 将导致 spring-data 将 , order_column ASC/DESC LIMIT X OFFSET Y
放在 ORDER BY 1
之后(不会修改无论如何结果)。
所以结果应该是
@Query(value = "SELECT dense_rank() OVER (ORDER BY rank DESC)," +
"t.id, " +
"FROM table t WHERE t.x > 0 ORDER BY 1",
nativeQuery = true)
Page<Object[]> getT(Pageable pageable);