spring 数据 jpa @query 和 pageable [英] spring data jpa @query and pageable

查看:53
本文介绍了spring 数据 jpa @query 和 pageable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring Data JPA,当我使用 @Query 来定义查询时 WITHOUT Pageable,它有效:

I'm using Spring Data JPA, and when I use @Query to to define a query WITHOUT Pageable, it works:

public interface UrnMappingRepository extends JpaRepository<UrnMapping, Long> {
    @Query(value = "select * from internal_uddi where urn like %?1% or contact like %?1%", 
           nativeQuery = true)
    List<UrnMapping> fullTextSearch(String text);
}

但如果我添加第二个参数 Pageable@Query 将不起作用,Spring 将解析方法的名称,然后抛出 异常 未找到完整的属性.这是一个错误吗?

But if I add the second param Pageable, the @Query will NOT work, and Spring will parse the method's name, then throw the exception No property full found. Is this a bug?

public interface UrnMappingRepository extends JpaRepository<UrnMapping, Long> {
    @Query(value = "select * from internal_uddi where urn like %?1% or contact like %?1%",
           nativeQuery = true)
    Page<UrnMapping> fullTextSearch(String text, Pageable pageable);
}

推荐答案

一个类似的问题是 在 Spring 论坛上提问,有人指出要应用分页,必须派生第二个子查询.因为子查询引用相同的字段,所以您需要确保您的查询对其引用的实体/表使用别名.这意味着你写的地方:

A similar question was asked on the Spring forums, where it was pointed out that to apply pagination, a second subquery must be derived. Because the subquery is referring to the same fields, you need to ensure that your query uses aliases for the entities/tables it refers to. This means that where you wrote:

select * from internal_uddi where urn like

你应该改为:

select * from internal_uddi iu where iu.urn like ...

这篇关于spring 数据 jpa @query 和 pageable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆