Benefits of Stream and Spring Data

Some people override the CrudRepository findAll method to return Stream (java 8), but I saw that they finally convert Stream to a list to send it through the break controller. Why do they use Stream? What is the advantage of using Stream here? If they want to filter records, I think it is better to filter in DataBase.

+4
source share
2 answers

This is already supported in Spring JPA data, see here ; therefore there is no real advantage to overriding those that return Stream. If you really want Streamsome of the potential benefits that come with it, use what Spring Data JPA already provides.

And another aspect is that JPA Spec 2.2it may be the default return type of some queries. Interfaces JPA Queryand TypedQueryreceive a new method called getResultStream().

So Spring Data will use provider-specific methods, such as Hibernateor EclipseLinkto stream the result.

getResultStream - list.stream, Hibernate ScrollableResult. , .

+4

, Stream s.

  • - , , Stream .

  • "". -, , . , Stream s. .

+2

Source: https://habr.com/ru/post/1679684/


All Articles