【问题标题】:How to limit result in @Query used in Spring Data Repository如何限制 Spring Data Repository 中使用的 @Query 的结果
【发布时间】:2018-05-16 22:07:46
【问题描述】:

我正在 Spring Data JPA 中通过CrudRepository 检索数据。我想过滤从@Query 注释中提供的自定义查询中检索到的记录。我为select rows.. 尝试了.setMaxResults(20);,但它给出了错误。我想从我的表中过滤前 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);

}

我希望我的 List&lt;SLSNotification&gt; getApplicationsByUserIdforManage(@Param("userId") String userId); 方法检索有限的数据集。我怎样才能打电话给实体经理或其他东西或任何东西来做到这一点?

请帮我做这件事。

【问题讨论】:

  • 为什么不在你的 hql 中使用limit ??
  • 我尝试添加限制。但在 hql 中它不起作用...你确定它有效
  • 告诉我们你是如何在你的总部尝试limit的?如果它不起作用。什么错误或异常?
  • @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限制 20")
  • 当你尝试一些原生查询时,你应该把nativeQuery = true放在@Query中。你试过吗?

标签: hibernate spring-data spring-data-jpa hql


【解决方案1】:

您可以在 SQL 中通过limitstatement 提供限制。并在@Query 注释中使用nativeQuery = true 来设置JPA 提供程序(如Hibernate)将其视为native 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);

如果您还不知道,Spring Data JPA 从方法名称构造查询。很神奇,对吧?阅读此documentation 以获得更好的理解。

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

Pageable topTwenty = PageRequest.of(0, 20);
List<SLSNotification> notifications = repository.findByUserIdOrderBySNumber("101", topTwenty);

此外,如果您使用的是 Java 8

您可以选择在界面中使用 默认方法,让生活更轻松

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

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

【讨论】:

  • 好的,我会这样做.....谢谢您的评论...请等我检查
  • 我添加但限制不起作用..它仍然返回我以前的查询并且不允许 desc order
  • limit 是一条 SQL 语句。它显然可以在工作台上工作
  • 你能给出你的异常的堆栈跟踪吗?
  • 好的。很高兴听到。但是还有更多其他方法可以做同样的事情。保持关注。我正在更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 2016-02-23
相关资源
最近更新 更多