流和Spring数据的优势 [英] Advantages of Stream and Spring Data

查看:63
本文介绍了流和Spring数据的优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些人重写了CrudRepository的方法findAll以返回一个Stream(java 8),但是我看到他们最终将Stream转换为List,以便通过rest控制器发送它.他们为什么要使用Stream?在此处使用Stream的好处是什么?如果他们想过滤记录,我认为最好在数据库上过滤.

Some people override the CrudRepository's method findAll to return an Stream (java 8), but I saw they finally transform the Stream to a List in order to send it through a rest controller. Why are they using a Stream ? What is the advantage to use a Stream here? If they want to filter the records I think would be better filter on DataBase.

推荐答案

Spring Data JPA已支持此功能,请查看

This is already supported in Spring Data JPA, look here; so there's not real advantage to override those to return Stream. If you really want a Stream and some potential advantages that would come with it - use what already Spring Data JPA provides.

还有一个不同的方面是,在JPA Spec 2.2中,这可能是某些查询的默认返回类型. JPA接口QueryTypedQuery将获得一个名为getResultStream()的新方法.

And also a different aspect is that in JPA Spec 2.2 this could be the default return type of some queries. The JPA interfaces Query and TypedQuery will get a new method called getResultStream().

因此,Spring Data将使用特定于特定提供程序的技术,例如HibernateEclipseLink来流式传输结果.

So Spring Data will use techniques specific to a particular provider, like Hibernate or EclipseLink to stream the result.

默认情况下,getResultStream只是一个list.stream实现,但是Hibernate已经使用ScrollableResult覆盖了它.如果您需要处理非常大的结果集,这种方法会更有效率.

By default getResultStream is just a list.stream implementation, but Hibernate already overrides that with ScrollableResult. This is way more efficient if you need to process a very big result set.

这篇关于流和Spring数据的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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