Advantages of Stream and Spring Data

前端 未结 3 1184
灰色年华
灰色年华 2021-02-20 12:13

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

3条回答
  •  醉梦人生
    2021-02-20 13:09

    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.

    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().

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

    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.

提交回复
热议问题