Spring Data PageImpl没有返回正确大小的页面? [英] Spring Data PageImpl not returning page with the correct size?

查看:522
本文介绍了Spring Data PageImpl没有返回正确大小的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从数据库中检索到的对象列表来创建新的Page.首先,我从数据库中获取所有元素,将其转换为Stream,然后使用lambda过滤结果.然后,我需要一个具有一定数量元素的Page,但是,实例化一个新的PageImpl似乎并不会返回具有正确大小的页面.

I am trying to create a new Page using a list of objects retrieved from the database. First I get all the elements from the DB, convert it to a Stream and then use lambda to filter the results. Then I need a Page with a set number of elements, however, instantiating a new PageImpl doesn't seem to return a page with the correct size.

这是我的代码:

List<Produtos> listaFinal;
Stream<Produtos> stream = produtosRepository.findAll().stream();
listaFinal = stream.filter(p -> p.getProdNome().contains("uio")).collect(Collectors.toList());

long total = listaFinal.size();
Page<Produtos> imp = new PageImpl<>(listaFinal,pageable,total);

这是调试的屏幕截图:

请注意,Pageable对象中的大小设置为20,并且它理解需要4个页面来呈现70个元素,但是它返回了整个列表.

Note the size in the Pageable object is set to 20 and it understands that it needs 4 pages to render the 70 elements, but it returns the whole list.

我想念什么?

编辑以回答托马斯的评论:

我了解如何使用Page返回仅一部分数据.我显示的代码是我尝试使用lambda表达式来过滤我的集合的尝试.对我来说,问题是我想使用Java 8的lambda通过Spring Data JPA查询数据库.我曾经使用过VB.NET和Entity function(x)查询表达式,并且想知道如何使用Spring JPA做到这一点.

I understand how to use Page to return just a slice of the data. The code I showed was my attempt to use a lambda expression to filter my collection. The problem for me is I want to use Java 8's lambda to query the database via Spring Data JPA. Im used to VB.NET's and Entity function(x) query expressions and was wondering how to do the same with Spring JPA.

在我的存储库中,Im使用extends JpaRepository<Produtos, Integer>, QueryDslPredicateExecutor<Produtos>,这使我可以访问findAll(Predicate,Pageable).但是,没有输入谓词,所以我不能在查询中简单地使用p -> p.getProdNome().contains("uio").我正在使用SQL Server和Hibernate.

In my repository, Im using extends JpaRepository<Produtos, Integer>, QueryDslPredicateExecutor<Produtos> which gives me access to findAll(Predicate,Pageable). However, the Predicate is not typed so I cant simply use p -> p.getProdNome().contains("uio") in the query. I'm using SQL Server and Hibernate.

推荐答案

了解了有关Spring Data工作原理的更多信息后,我最终在JpaRepository实现内的方法上使用了@Query批注,以正确查询数据库并过滤结果,无需使用流,然后再转换回Page.

After learning more about how Spring Data works I ended up using @Query annotations on my methods inside the JpaRepository implementations to properly query the DB and filter the results, eliminating the need to use a stream and then convert back to Page.

这是上面的代码在万一有人需要示例的情况下的样子:

Here's how the code above would look in case anyone needs an example:

@Query("select p from Produtos p where p.prodNome = ?1")
public Page<Produtos> productsListByName(String prodNome, Pageable pageable)

我知道Spring的findBy方法,但是有时根据参数的数量,很难真正读取方法名称,所以我只是坚持使用JPQL.

Im aware of Spring's findBy methods but sometimes the method names become really difficult to read depending on the amount of parameters so I just stuck to JPQL.

通过这种方式,页面的内容将始终具有您在Spring配置中定义的最大元素数量.

Doing it this way the Page's content will always have up to the maximum amount of elements defined by you in the Spring configuration.

我还使用PageImpl的自定义实现,我现在不在工作并且无法访问代码,但是我会尽可能地将其发布.

I also use a custom implementation of PageImpl, I'm not at work right now and don't have access to the code, but I'll post it whenever I can.

可以在此处

这篇关于Spring Data PageImpl没有返回正确大小的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆