简体   繁体   English

Spring Data 和 Native Query with Sorting

[英]Spring Data and Native Query with Sorting

In a web project, using spring-data(1.10.4.RELEASE) with a Oracle database, i am trying use a native query with a Sort variable.在 web 项目中,使用 spring-data(1.10.4.RELEASE) 和 Oracle 数据库,我尝试使用带有 Sort 变量的本机查询。

public interface UserRepository extends JpaRepository<User, Long> {
  @Query(nativeQuery = true,value = "SELECT * FROM USERS WHERE LASTNAME = :lastname #sort")
  List<User> findByLastname(@Param("lastname") String lastname, Sort sort);
}

The query launched is:启动的查询是:

SELECT * FROM USERS WHERE LASTNAME = 'Lorite' #sort ORDER BY LASTNAME

Like you can see the annotation "#sort" is still there.就像您可以看到注释“#sort”仍然存在一样。

I have tried Spring Data and Native Query with pagination but the annotation is there yet and using another syntax like ?#{#sort} or {#sort} the problem persist.我已经尝试了带有分页的 Spring Data 和 Native Query,但注释还在那里,并且使用另一种语法,如 ?#{#sort} 或 {#sort} 问题仍然存在。

Anything is welcome.什么都欢迎。

Thanks!谢谢!

The documentation says:文档说:

Note, that we currently don't support execution of dynamic sorting for native queries as we'd have to manipulate the actual query declared and we cannot do this reliably for native SQL.请注意,我们目前不支持对本机查询执行动态排序,因为我们必须操作声明的实际查询,而我们无法对本机 SQL 可靠地执行此操作。

Furthermore, this #sort interpolation does not exist此外,这个#sort 插值不存在

[1] http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ [1] http://docs.spring.io/spring-data/jpa/docs/current/reference/html/

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
    countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
    nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);
}

Example 64. Declare native count queries for pagination at the query method by using @Query 示例 64. 使用 @Query 在查询方法中声明用于分页的本机计数查询

Native Queries with Spring Data JPA 使用 Spring Data JPA 的本机查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM