Duplicate results when sorting using a Spring-Data pageable object on a JPA repository
up vote
3
down vote
favorite
I have a rest-api that returns a list of users when called. The API uses the org.springframework.data.domain.Pageable to paginate and sort the results. This works by simply passing the pageable to the JPA repository, which then returns the desired page.
For some reason, when sorting by the first name, there is a chance that a duplicate entry will appear, but only if multiple entries have the same first name. However, this never happens when sorting by lastName. Both are simply strings in the entity, there is no discernable difference besides the property name.
Have any of you ever encountered this and if so, how did you fix it?
EDIT: To clarify, there is basically no logic of mine between the controller and the repository. I just pass the pageable through and return the results.
EDIT 2: Solved! Interesting titbit: The reason why the issue was only occurring when sorting by the first name was that only there were there always records that appeared on page 1 and 2, regardless which way the entries were sorted. Lucky me that our testers used that specific test data, or I might have never noticed.
spring spring-data-jpa spring-data
add a comment |
up vote
3
down vote
favorite
I have a rest-api that returns a list of users when called. The API uses the org.springframework.data.domain.Pageable to paginate and sort the results. This works by simply passing the pageable to the JPA repository, which then returns the desired page.
For some reason, when sorting by the first name, there is a chance that a duplicate entry will appear, but only if multiple entries have the same first name. However, this never happens when sorting by lastName. Both are simply strings in the entity, there is no discernable difference besides the property name.
Have any of you ever encountered this and if so, how did you fix it?
EDIT: To clarify, there is basically no logic of mine between the controller and the repository. I just pass the pageable through and return the results.
EDIT 2: Solved! Interesting titbit: The reason why the issue was only occurring when sorting by the first name was that only there were there always records that appeared on page 1 and 2, regardless which way the entries were sorted. Lucky me that our testers used that specific test data, or I might have never noticed.
spring spring-data-jpa spring-data
Are you 100% sure that this duplicate entry is indeed duplicate AND does not exist in the database?
– Sven Hakvoort
Nov 22 at 15:58
How did the multiple entries appear? On the same page? On two consecutive pages?
– Tom
Nov 22 at 16:41
1
Possible duplicate of Paging in SQL with LIMIT/OFFSET sometimes results in duplicates on different pages
– Alan Hay
Nov 22 at 17:37
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a rest-api that returns a list of users when called. The API uses the org.springframework.data.domain.Pageable to paginate and sort the results. This works by simply passing the pageable to the JPA repository, which then returns the desired page.
For some reason, when sorting by the first name, there is a chance that a duplicate entry will appear, but only if multiple entries have the same first name. However, this never happens when sorting by lastName. Both are simply strings in the entity, there is no discernable difference besides the property name.
Have any of you ever encountered this and if so, how did you fix it?
EDIT: To clarify, there is basically no logic of mine between the controller and the repository. I just pass the pageable through and return the results.
EDIT 2: Solved! Interesting titbit: The reason why the issue was only occurring when sorting by the first name was that only there were there always records that appeared on page 1 and 2, regardless which way the entries were sorted. Lucky me that our testers used that specific test data, or I might have never noticed.
spring spring-data-jpa spring-data
I have a rest-api that returns a list of users when called. The API uses the org.springframework.data.domain.Pageable to paginate and sort the results. This works by simply passing the pageable to the JPA repository, which then returns the desired page.
For some reason, when sorting by the first name, there is a chance that a duplicate entry will appear, but only if multiple entries have the same first name. However, this never happens when sorting by lastName. Both are simply strings in the entity, there is no discernable difference besides the property name.
Have any of you ever encountered this and if so, how did you fix it?
EDIT: To clarify, there is basically no logic of mine between the controller and the repository. I just pass the pageable through and return the results.
EDIT 2: Solved! Interesting titbit: The reason why the issue was only occurring when sorting by the first name was that only there were there always records that appeared on page 1 and 2, regardless which way the entries were sorted. Lucky me that our testers used that specific test data, or I might have never noticed.
spring spring-data-jpa spring-data
spring spring-data-jpa spring-data
edited Nov 23 at 10:36
Antu
546319
546319
asked Nov 22 at 15:05
Alex Eggers
9311
9311
Are you 100% sure that this duplicate entry is indeed duplicate AND does not exist in the database?
– Sven Hakvoort
Nov 22 at 15:58
How did the multiple entries appear? On the same page? On two consecutive pages?
– Tom
Nov 22 at 16:41
1
Possible duplicate of Paging in SQL with LIMIT/OFFSET sometimes results in duplicates on different pages
– Alan Hay
Nov 22 at 17:37
add a comment |
Are you 100% sure that this duplicate entry is indeed duplicate AND does not exist in the database?
– Sven Hakvoort
Nov 22 at 15:58
How did the multiple entries appear? On the same page? On two consecutive pages?
– Tom
Nov 22 at 16:41
1
Possible duplicate of Paging in SQL with LIMIT/OFFSET sometimes results in duplicates on different pages
– Alan Hay
Nov 22 at 17:37
Are you 100% sure that this duplicate entry is indeed duplicate AND does not exist in the database?
– Sven Hakvoort
Nov 22 at 15:58
Are you 100% sure that this duplicate entry is indeed duplicate AND does not exist in the database?
– Sven Hakvoort
Nov 22 at 15:58
How did the multiple entries appear? On the same page? On two consecutive pages?
– Tom
Nov 22 at 16:41
How did the multiple entries appear? On the same page? On two consecutive pages?
– Tom
Nov 22 at 16:41
1
1
Possible duplicate of Paging in SQL with LIMIT/OFFSET sometimes results in duplicates on different pages
– Alan Hay
Nov 22 at 17:37
Possible duplicate of Paging in SQL with LIMIT/OFFSET sometimes results in duplicates on different pages
– Alan Hay
Nov 22 at 17:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Yes, I have seen that: a record can appear on, say, page 1 and then again on page 2.
This is an issue at the database level. For example 10 items per page and items at positions 10 and 11 have the same value for the property then it can be random which one appears in which position in each resultset.
Therefore apply a secondary sort on a unique property - the database ID for example - in order to ensure consistent ordering across requests.
This was the issue. I'm actually annoyed it took me this long to see it...looking back there is obviously no reason the way entries with the same keys should be sorted consistently across multiple requests :D Thanks!
– Alex Eggers
Nov 23 at 9:33
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Yes, I have seen that: a record can appear on, say, page 1 and then again on page 2.
This is an issue at the database level. For example 10 items per page and items at positions 10 and 11 have the same value for the property then it can be random which one appears in which position in each resultset.
Therefore apply a secondary sort on a unique property - the database ID for example - in order to ensure consistent ordering across requests.
This was the issue. I'm actually annoyed it took me this long to see it...looking back there is obviously no reason the way entries with the same keys should be sorted consistently across multiple requests :D Thanks!
– Alex Eggers
Nov 23 at 9:33
add a comment |
up vote
1
down vote
accepted
Yes, I have seen that: a record can appear on, say, page 1 and then again on page 2.
This is an issue at the database level. For example 10 items per page and items at positions 10 and 11 have the same value for the property then it can be random which one appears in which position in each resultset.
Therefore apply a secondary sort on a unique property - the database ID for example - in order to ensure consistent ordering across requests.
This was the issue. I'm actually annoyed it took me this long to see it...looking back there is obviously no reason the way entries with the same keys should be sorted consistently across multiple requests :D Thanks!
– Alex Eggers
Nov 23 at 9:33
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Yes, I have seen that: a record can appear on, say, page 1 and then again on page 2.
This is an issue at the database level. For example 10 items per page and items at positions 10 and 11 have the same value for the property then it can be random which one appears in which position in each resultset.
Therefore apply a secondary sort on a unique property - the database ID for example - in order to ensure consistent ordering across requests.
Yes, I have seen that: a record can appear on, say, page 1 and then again on page 2.
This is an issue at the database level. For example 10 items per page and items at positions 10 and 11 have the same value for the property then it can be random which one appears in which position in each resultset.
Therefore apply a secondary sort on a unique property - the database ID for example - in order to ensure consistent ordering across requests.
edited Nov 23 at 9:35
answered Nov 22 at 17:37
Alan Hay
15.2k22668
15.2k22668
This was the issue. I'm actually annoyed it took me this long to see it...looking back there is obviously no reason the way entries with the same keys should be sorted consistently across multiple requests :D Thanks!
– Alex Eggers
Nov 23 at 9:33
add a comment |
This was the issue. I'm actually annoyed it took me this long to see it...looking back there is obviously no reason the way entries with the same keys should be sorted consistently across multiple requests :D Thanks!
– Alex Eggers
Nov 23 at 9:33
This was the issue. I'm actually annoyed it took me this long to see it...looking back there is obviously no reason the way entries with the same keys should be sorted consistently across multiple requests :D Thanks!
– Alex Eggers
Nov 23 at 9:33
This was the issue. I'm actually annoyed it took me this long to see it...looking back there is obviously no reason the way entries with the same keys should be sorted consistently across multiple requests :D Thanks!
– Alex Eggers
Nov 23 at 9:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433735%2fduplicate-results-when-sorting-using-a-spring-data-pageable-object-on-a-jpa-repo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you 100% sure that this duplicate entry is indeed duplicate AND does not exist in the database?
– Sven Hakvoort
Nov 22 at 15:58
How did the multiple entries appear? On the same page? On two consecutive pages?
– Tom
Nov 22 at 16:41
1
Possible duplicate of Paging in SQL with LIMIT/OFFSET sometimes results in duplicates on different pages
– Alan Hay
Nov 22 at 17:37