Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data findBy return type

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.

like image 319
krmanish007 Avatar asked Jul 01 '16 14:07

krmanish007


People also ask

What is the return type of findById in JPA?

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.

What does JPA saveAll return?

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.

What is Findby in JPA?

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.

What is the return type of findAll method in CrudRepository?

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.


2 Answers

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

like image 94
Rodrigo Engelberg Avatar answered Sep 21 '22 05:09

Rodrigo Engelberg


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:

  1. a (Optional) single item, could be a primitive, wrapped, or object, or void
  2. a collection of items, or paged list
  3. a stream, Iterator, Mono/Flux, or those similar.
  4. Future, or its variants

And, the complete list of currently supported return types:

  • 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
like image 26
ryenus Avatar answered Sep 22 '22 05:09

ryenus