Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA slice underlying SQL

I was reading this paragraph in the Spring Data JPA official documentation about the difference between Page and Slice (emphasis mine):

The first method lets you 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), you can instead return a Slice. A Slice only knows about whether a next Slice is available, which might be sufficient when walking through a larger result set.

I get it how a Page can get the total elements by executing an additional count query with the same where clause, but how does a Slice gets to know whether other elements are available or not with a single query? What is the SQL executed?

like image 605
Aurasphere Avatar asked May 16 '19 20:05

Aurasphere


People also ask

How do I use JPA with spring data JPA?

annotation to specify a custom JPQL or native SQL query. Spring Data JPA provides the required JPA code to execute the statement as a JPQL or native SQL query. Your preferred JPA implementation, such as, Hibernate or EclipseLink, will then execute the query and map the result.

What is slice in Spring Boot with example?

Slice is a sized chunk of data with an indication of whether there is more data available. Spring supports Slice as a return type of the query method. Pageable parameter must be specified by the same query method. Slice avoids triggering a count query to calculate the overall number of pages as that might be expensive.

How do I use SQL in spring data repository?

In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm.xml file.

How do I use @query in spring?

Select Query In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm.xml file.


1 Answers

How does a Slice gets to know whether other elements are available or not with a single query?

Spring Data JPA selects one element more than actually needed to fill the Slice if that additional element is present, there is another Slice available. If not this is the last Slice.

See the code.

like image 129
Jens Schauder Avatar answered Sep 28 '22 15:09

Jens Schauder