question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pageable.size returns 0 for non-empty Page queried from JpaRepository with Pageable.unpaged()

See original GitHub issue

Versions: Spring Boot version: 2.1.2.RELEASE Kotlin version: 1.3.21 I am using Envers via data-spring-envers. (I don’t think Envers makes any changes to how Paging works, but I mention it just to be complete.)

Hello, I don’t know if this is the right place to fill this issue and I couldn’t find anything similar in the existing ones. The problem is: Calling size (inherited from Slice) on Page<T> instance will always return 0, even if the page has items.

The following test code will trigger an assert:

// (code from working project, but reduced to make smaller test case)
// repository
// src/main/kotlin/example/repositories/LocationRepository.kt
package example.repositories

import example.entities.Location
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.data.repository.history.RevisionRepository

interface LocationRepository : JpaRepository<Location, Long>, JpaSpecificationExecutor<Location>, RevisionRepository<Location, Long, Int> {
    fun findByCodeIgnoreCaseContaining(code: String, paging: Pageable): Page<Location>
}

// entity
// src/main/kotlin/example/entities/Location.kt
package example.entities

import org.hibernate.envers.Audited
import org.hibernate.envers.RelationTargetAuditMode
import javax.persistence.*

@Entity
@Table(name = "location")
@Audited
class Location {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    var id: Long? = null

    @Column(name = "code")
    var code: String = ""
    // ...
}

// test
// src/test/kotlin/example/specifications/LocationSpecificationTest.kt
package example.specifications

import example.config.JpaEnversConfiguration
import example.config.PaginationConfig
import example.config.repositories.LocationRepository
import example.utils.SearchCriteria
import org.hibernate.envers.AuditOverride
import org.junit.Test

import org.junit.Assert.*
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.context.annotation.Import
import org.springframework.data.domain.Pageable
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit4.SpringRunner

@RunWith(SpringRunner::class)
@ActiveProfiles("test")
@DataJpaTest
@Import(JpaEnversConfiguration::class)
class LocationSpecificationTest {
    @Autowired
    lateinit var locationRepository: LocationRepository

    @Test
    fun sanityCheckForLocationsNonExisting() {
        val locations = locationRepository.findByCodeIgnoreCaseContaining("NONEXISTING", Pageable.unpaged())
        println("locations.size == 0: ${locations.size == 0}")
        println(locations)
        locations.forEach {
            println(it)
        }
        assertTrue(locations.isEmpty)
        assertTrue(locations.totalElements == 0.toLong())
        assertTrue(locations.size == 0) // this is OK
        assertTrue(locations.totalElements == locations.size.toLong())
    }

    @Test
    fun sanityCheckForLocationsExisting() {
        val locations = locationRepository.findByCodeIgnoreCaseContaining("L", Pageable.unpaged())
        println("locations.size == 0: ${locations.size == 0}")
        println(locations)
        locations.forEach {
            println(it)
        }
        assertFalse(locations.isEmpty)
        assertFalse(locations.totalElements == 0.toLong())
        // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        assertFalse(locations.size == 0) // <<<---- THIS LINE FAILS
        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        assertTrue(locations.totalElements == locations.size.toLong())
    }
}

// others
// src/main/kotlin/example/config/JpaEnversConfiguration.kt
package example.config

import org.springframework.context.annotation.Configuration
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean
import org.springframework.data.jpa.repository.config.EnableJpaRepositories

@Configuration
@EnableJpaRepositories(basePackages = ["example"], repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean::class)
class JpaEnversConfiguration

Result: sanityCheckForLocationsNonExisting: passed sanityCheckForLocationsExisting: assertion error.

locations.size == 0: true
Page 1 of 1 containing example.entities.Location instances
#<Location:87682515, id: 1, code: "L">

java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertFalse(Assert.java:64)
at org.junit.Assert.assertFalse(Assert.java:74)
at example.specifications.LocationSpecificationTest.sanityCheckForLocationsExisting(LocationSpecificationTest.kt:53)
*/

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
daliborfiluscommented, Feb 12, 2019

Yes, I understand that, but this definitely looked like a bug. If I had doubts about that, I would ask on SO. But if you say this is not a bug, even if it walks, talks and acts like a bug, but an intended behavior, then I’m fine with that.

0reactions
wilkinsonacommented, Feb 12, 2019

Spring Data issues are tracked using JIRA. Issues for Spring Data Commons, where the paging logic resides, are here. That said, an issue isn’t really the right place to raise something that you don’t understand. Stack Overflow or Gitter (there’s a Spring Data room) are both better places.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Data Pageable: Returns empty content - Stack Overflow
Ensure that your PageRequest object is requesting 0 for small sets, not 1 . The pagination begins from 0 . This is a...
Read more >
Spring Data Web Support - Baeldung
Since the method takes a Pageable instance, it returns a subset of the entire set of entities, stored in a Page<User> object. A...
Read more >
Spring Data JDBC - Reference Documentation
unpaged() . The first method lets you pass an org.springframework.data.domain.Pageable instance to the query method to dynamically add ...
Read more >
Paging and Sorting with Spring Data JPA | by Thanh Tran
If the query returns 1000 entries then we set 10 entries for a page we will have a total of 100 pages. Now...
Read more >
Spring Data JPA Pagination - Dan Vega
In this tutorial, you are going to learn how to work with pagination in Spring Data JPA. If you have a few records...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found