Java 使用 Spring Data JPA 和 @Query 注释仅获取第一个/最后一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32459618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:35:01  来源:igfitidea点击:

Fetching only first/last element using Spring Data JPA and @Query annotation

javaspringspring-data-jpajpql

提问by Ueli Hofstetter

EDIT:Solutions to this problem are provided in the second and fourth answer regarding this question setMaxResults for Spring-Data-JPA annotation?

编辑:关于这个问题的第二个和第四个答案中提供了这个问题的解决方案setMaxResults for Spring-Data-JPA annotation?

Goal:Fetch the largest/smallest element by property z using a Spring Data JPA repository and the Spring Query annotation.

目标:使用 Spring Data JPA 存储库和 Spring Query 注释按属性 z 获取最大/最小元素。

What I have so far

到目前为止我所拥有的

@Query("SELECT xelement FROM x xelement ORDER BY xelement.z")
public List<X> findFirstElement();

Problem:This query fetches all elements (which is not really effective). If I would use the EntityManager direcly, I could set the number of results using

问题:此查询获取所有元素(这不是真正有效)。如果我直接使用 EntityManager,我可以使用

entityManager.setMaxResults(1)

to only get the first element.

只获取第一个元素。

Question:How do I specify the maximum number of results using the @Queryannotation?

问题:如何使用@Query注释指定最大结果数?

Idea:Is using a PageRequest of size 0 the way to go?

想法:使用大小为 0 的 PageRequest 是否可行?

Constraints:I am aware of the "FindFirstBy...." query feature but I want/have to use the @Queryannotation.

约束:我知道“FindFirstBy....”查询功能,但我想/必须使用@Query注释。

回答by fjgarzon

Try to do this:

尝试这样做:

@Query(value = "SELECT xelement FROM x xelement ORDER BY xelement.z  LIMIT 1",
       nativeQuery = true)

回答by ahmetcetin

You can use the limit property of sql just by adding nativeQuery to @Query annotation. However, there is another and a better way of doing this. Pageable class in your repository method will solve your problem without touching your @Query annotation:

只需将 nativeQuery 添加到 @Query 注释中,就可以使用 sql 的 limit 属性。但是,还有另一种更好的方法可以做到这一点。您的存储库方法中的 Pageable 类将解决您的问题,而无需触及您的 @Query 注释:

@Query(value = "SELECT xelement FROM x xelement ORDER BY xelement.z")
List<X> findFirstElement(Pageable limit);

To set the limit and offset, use should call this repository method as follows:

要设置限制和偏移量,使用应调用此存储库方法,如下所示:

List<X> xValues = xRepository.findFirstElement(new PageRequest(0, 1));

Here 1 corresponds to the limit which you want.

这里 1 对应于您想要的限制。

UPDATE (SPRING DATA 2.0)

更新(春季数据 2.0)

Use PageRequest.of(0, 1)instead of new PageRequest(0, 1)

使用PageRequest.of(0, 1)而不是new PageRequest(0, 1)

回答by comiventor

The closest JPA query syntax I can think for your use case is findFirstByZIsNotNullOrderByZAsc. This should eliminate the need to write custom native query.

我可以为您的用例想到的最接近的 JPA 查询语法是findFirstByZIsNotNullOrderByZAsc. 这应该消除编写自定义本机查询的需要。