流和Spring数据的优势 [英] Advantages of Stream and Spring Data
问题描述
有些人重写了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.
推荐答案
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接口Query
和TypedQuery
将获得一个名为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将使用特定于特定提供程序的技术,例如Hibernate
或EclipseLink
来流式传输结果.
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屋!