Search code examples
javaspring-data-jpapageable

Can one get repeated results on an unsorted page?


if I request for a Page of results from my DB without any ordering provided

val page1 = repository.findAll(PageRequest.of(1,10))
val page2 = repository.findAll(PageRequest.of(2,10))

Could page1 and page2 contain the same elements since I didn't specify an order? Or could page1 contain different elements in different invocations despite the DB contents not changing?


Solution

  • It's unlikely you'd get repeated elements if the database's contents haven't changed, but you should never rely on this behavior. Any number of factors may affect the order of rows returned from an unordered query (e.g., some background process performing a vacuum, presence of some rows in a cache), which may cause different pages to contain the same elements.