What are the possible return types of spring data findBy methods? I haven't found any consolidated list anywhere.
like findByXXX can return
1) Entity
2) Optional<Entity>
3) List<Entity>
4) Stream<Entity>
and many more, but do we have a definite list?
Also, List<Entity>
returns emptyList when no data found but Stream<Entity>
returns null when no data found, so when you apply map/filter etc on stream, you get NPE.
Is there a better way to use Stream without causing NPE, other than physically checking null before calling stream functions.
Its findById method retrieves an entity by its id. The return value is Optional<T> . Optional<T> is a container object which may or may not contain a non-null value. If a value is present, isPresent returns true and get returns the value.
Spring Data JPA CrudRepository - saveAll() Method As the name depicts, the saveAll() method allows us to save multiple entities to the DB. It belongs to the CrudRepository interface defined by Spring Data.
JPA finder methods are the most powerful methods, we can create finder methods to select the records from the database without writing SQL queries. Behind the scenes, Data JPA will create SQL queries based on the finder method and execute the query for us.
If you make StudentRepository inherit from JpaRepository you have the findAll() method by returning a List. If you want to read why CrudRepository returns an Iterable by default: stackoverflow.com/a/31897487/1868371.
Your list of possible return type is corret. You can use too the type Page and Slice when you need pagination the result. See example:
Page<User> findByLastname(String lastname, Pageable pageable);
Slice<User> findByLastname(String lastname, Pageable pageable);
"The first method allows you to pass an org.springframework.data.domain.Pageable instance to the query method to dynamically add paging to your statically defined query. A Page knows about the total number of elements and pages available. It does so by the infrastructure triggering a count query to calculate the overall number. As this might be expensive depending on the store used, Slice can be used as return instead. A Slice only knows about whether there’s a next Slice available which might be just sufficient when walking thought a larger result set." - See more in: Spring Data Documentation
For another question, you can use Guava/Java 8 Optional. This way, your query method will return an Optional that contains the found object or an empty Optional. Optional is a way of replacing a nullable T reference with a non-null value, see example:
Optional<User> findById(Long id);
I hope I have help you. =)
Though a bit late, but the supported return types are documented here:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-return-types
Generally, the return types could be any of the following categories:
- void
- Primitives
- Wrapper types
- T
- Iterator
- Collection
- List
- Optional
- Option
- Stream
- Streamable
- Types that implement Streamable and take a Streamable constructor or factory method argument
- Vavr Seq, List, Map, Set
- Future
- CompletableFuture
- ListenableFuture
- Slice
- Page
- GeoResult
- GeoResults
- GeoPage
- Mono
- Flux
- Single
- Maybe
- Flowable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With