本文介绍了具有限制和偏移量的JPQL查询注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些抽象方法的存储库接口,在其中我使用了@Query注释.现在,我想为该查询添加限制和偏移量支持.

I have a repository interface with some abstract methods where I use the @Query annotation.Now I would like to add limit and offset support to this queries.

示例:

public interface ProductRepository
   extends CrudRepository<Product, Long> {

    @Query("from Product")
    List<Product> findAllProducts();
}

这样的东西会很好

public interface ProductRepository
   extends CrudRepository<Product, Long> {

    @Query("from Product limit :limit ")
    List<Product> findAllProducts(@Param("limit") Integer limit);
}

但这不起作用.有一个解决方案,我创建了该接口的实现(http://stackoverflow.com/questions/3479128/jpql-limit-number-of-results)但是我想知道是否不可能在查询中添加偏移量和限制,或者是否对此有注释.

But this doesn't work. There is a solution that I create an implementation of the interface (http://stackoverflow.com/questions/3479128/jpql-limit-number-of-results)But I wonder if there is not a possibility of adding offset and limit to the query or if there is an annotation for this.

推荐答案

limit.即使没有它,您的查询也是无效的 JPQL查询(但可能是有效的HQL,并且如果您的JPA提供者可以容忍,则可以使用.)

limit is not supported by JPQL. Even without it your queries are not valid JPQL queries (but may be valid HQL - and may work if your JPA provider is tolerant).

需要(部分)实现,因此您可以使用Query界面或标准api.

A (partial) implementation is needed so you can use the Query interface or the criteria api.

这篇关于具有限制和偏移量的JPQL查询注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:51
查看更多