Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicate records fetched while using findall method with specification and payable #6

Open
nagraj321 opened this issue Dec 21, 2020 · 4 comments

Comments

@nagraj321
Copy link

nagraj321 commented Dec 21, 2020

I'm seeing some weird issue while working with findAll() method with the specification and pageable arguments. If I set the lower page i.e. 0 size i.e. 10 then I don't have any issues. but if I set the page i.e. 0 and size to 30 I'm getting the duplicate records.

e.g.

ID NAME
1 test
2 test
3 test
4 test
5 test
6 test
7 test
8 test
9 test
1 test
2 test
3 test
4 test
11 test
6 test
7 test
18 test

I have the following in the database.

ID NAME
1 test
2 test
3 test
4 test
5 test
6 test
7 test
8 test
9 test
10 test
11 test
12 test
13 test
14 test
15 test
16 test
17 test
18 test

@Entity
@Table(name = "table_name")
@JsonIgnoreProperties(ignoreUnknown = true)
@DynamicUpdate
public class Emp implements Persistable<String> {

    @Id
    @Column(name = "emp_id", nullable = false, columnDefinition = "BINARY(16)")
    public String empId;
    
    @Column(name = "name");
    public String name;
    // other fields
    
    //setters and getters
    
}
@Repository
public interface EmpRepository extends JpaRepository<Emp, String> {
    Page<Emp> findAll(Specification<Emp> specification, Pageable page);
}
public class EmpSpecification {
    public static Specification<Emp> nameEquals(String name) {
        return (root, query, builder) -> name == null ? null : builder.equal(root.get("name"), name);
    }
}

in the service class we are trying to fetch data using the following code:

Specification<Emp> specifications = Specification.where(EmpSpecification.nameEquals(name)));
details = consolidatedTradeSummaryRepository.findAll(specifications, page);

http://localhost:8080/emp?name=test&page=0&size=10
http://localhost:8080/emp?name=test&page=0&size=20

Spring boot version : 2.2.2.RELEASE

@AnghelLeonard
Copy link
Owner

Thank you for the comment. Well, most probably the issue is caused by a missing explicit sort. Try to add sort by ID (I assume this is a primary key), since the "name" has duplicates. I hope this helps.

@nagraj321
Copy link
Author

Thanks for your response. I have tried by providing the sort option but still getting the same response.

@AnghelLeonard
Copy link
Owner

Ok, I'll look into it. Please, tell me what database vendor you used for this test?

@nagraj321
Copy link
Author

I have used mysql database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants