【发布时间】: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<SLSNotification> 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