Hibernate 如何在Spring数据存储库中使用@Query限制结果

Hibernate 如何在Spring数据存储库中使用@Query限制结果,hibernate,spring-data,spring-data-jpa,hql,Hibernate,Spring Data,Spring Data Jpa,Hql,我正在通过Spring data JPA中的Crudepository检索数据。我想筛选从@query annotation中提供的自定义查询中检索的记录。我试过了。setMaxResults20;对于“选择行”。。但它会带来错误。我想筛选表中的前20行 这是我的存储库 package lk.slsi.repository; import java.util.Date; import lk.slsi.domain.SLSNotification; import org.springframew

我正在通过Spring data JPA中的Crudepository检索数据。我想筛选从@query annotation中提供的自定义查询中检索的记录。我试过了。setMaxResults20;对于“选择行”。。但它会带来错误。我想筛选表中的前20行

这是我的存储库

package lk.slsi.repository;

import java.util.Date;
import lk.slsi.domain.SLSNotification;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * Created by ignotus on 2/10/2017.
 */
public interface SLSNotificationRepository extends CrudRepository<SLSNotification, Integer> {


    @Override
    SLSNotification save(SLSNotification slsNotification);

    @Override
    SLSNotification findOne(Integer snumber);

    @Override
    long count();

    @Override
    void delete(Integer integer);

    @Override
    void delete(SLSNotification slsNotification);

    @Override
    void delete(Iterable<? extends SLSNotification> iterable);

    @Override
    List<SLSNotification> findAll();

    @Query("select a from SLSNotification a where a.slsiUnit in :unitList order by snumber desc")
    List<SLSNotification> getApplicationsByUnit(@Param("unitList") List<String> unitList);

    @Query("select a from SLSNotification a where a.userId = :userId")
    List<SLSNotification> getApplicationsByUserId(@Param("userId") String userId);

    @Query("select a.snumber, a.date, a.slsNo, a.slsiUnit, a.productDesc, a.status from SLSNotification a where a.userId = :userId ORDER BY snumber desc")
    List<SLSNotification> getApplicationsByUserIdforManage(@Param("userId") String userId);

    @Query("select a from SLSNotification a where a.slsNo = :slsNo")
    SLSNotification getApplicationBySLSNumber(@Param("slsNo") String slsNo);

}
我要我的名单getApplicationsByUserIdforManage@ParamuserId字符串用户标识;方法检索有限的数据集。我怎样才能打电话给实体经理或其他人来做这件事

请帮我做这个。

您可以通过SQL中的LIMIT语句提供限制。并在@查询注释中使用nTyVQuale=真来设置JPA PosiersHibernate,将其视为本地SQL查询。

@Query(nativeQuery = true, value = "SELECT * FROM SLSNotification s WHERE s.userId = :userId ORDER BY snumber DESC LIMIT 20")
List<SLSNotification> getUserIdforManage(@Param("userId") String userId);
或 此外,如果您想利用Spring Data JPA的便捷功能, 您可以通过适当的方法命名来实现

List<SLSNotification> findByUserIdOrderBySNumber(@Param("userId") String userId, Pageable pageable);
如果您还不知道,SpringDataJPA将从方法名构造查询。太棒了,对吧?为了更好地理解,请阅读此文

现在只需像这样调用这个方法

Pageable topTwenty = PageRequest.of(0, 20);
List<SLSNotification> notifications = repository.findByUserIdOrderBySNumber("101", topTwenty);
此外,如果您使用的是Java8 您可以选择在接口中使用默认方法,从而使生活更轻松

 List<SLSNotification> findByUserIdOrderBySNumber(@Param("userId") String userId, Pageable pageable);

 default List<User> findTop20ByUserIdOrderBySNumber(String userId) {
    return findByUserIdOrderBySNumber(userId, PageRequest.of(0,20));
 }

为什么不在hql中使用limit?我尝试添加limit。但在hql中它不起作用…你确定它起作用吗?告诉我们你是如何在hql中尝试限制的?如果它不起作用。错误或异常是什么?@query从SLSNotification a中选择a.snumber、a.date、a.slsNo、a.slsiUnit、a.productDesc、a.status,其中a.userId=:userId ORDER BY snumber desc limit 20尝试某些本机查询时,应在@query中输入nativeQuery=true。你试过了吗?好的,我会的…谢谢你的评论…请等到我检查我添加的,但限制不起作用…它仍然返回我以前的查询,不允许desc orderlimit是SQL语句。它显然在工作台上工作。你能给出异常的堆栈跟踪吗?好的。很高兴听到。但是有更多的其他方法来做同样的事情。别动。我正在更新答案。