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

Problem when using Sort.by #3721

Open
oungsi2000 opened this issue Dec 18, 2024 · 2 comments
Open

Problem when using Sort.by #3721

oungsi2000 opened this issue Dec 18, 2024 · 2 comments
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged

Comments

@oungsi2000
Copy link

oungsi2000 commented Dec 18, 2024

this is my whole code
https://github.com/oungsi2000/jump-to-springboot

When I use JpaRepository with Sort.by and Pageable, I got some mismatch with this.

com.ll.jumptospringboot.domain.Answer.AnswerService.getList

public Page<Answer> getList(Question question, int page, String sortBy) {
        List<Sort.Order> sorts = new ArrayList<>();
        if (Objects.equals(sortBy, "mostVoted")) {
            sorts.add(Sort.Order.desc("voter"));
            sorts.add(Sort.Order.desc("createDate"));
            Pageable pageable = PageRequest.of(page,10, Sort.by(sorts));
            return this.answerRepository.findAllByQuestion(question, pageable);
        } else {
            sorts.add(Sort.Order.desc("createDate"));
            Pageable pageable = PageRequest.of(page,10, Sort.by(sorts));
            return this.answerRepository.findAllByQuestion(question, pageable);
        }
    }

com.ll.jumptospringboot.domain.Answer.Answer

public class Answer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(columnDefinition = "TEXT")
    private String content;

    @Column(length = 200)
    private String title;

    private LocalDateTime createDate;

    @ManyToOne
    private Question question;

    @ManyToOne
    private SiteUser author;

    private LocalDateTime modifyDate;

    @ManyToMany
    Set<SiteUser> voter;
}

when using 'sorts.add(Sort.Order.desc("voter"));', If there are two or more voters, this.a
nswerRepository.findAllByQuestion(question, pageable); -> this doesn't get all Answers, instead gets pagination - 1 Answers

this is the result when the code above 'if (Objects.equals(sortBy, "mostVoted"))~~' executed
asdfad

this is the result when the code above 'else~~' executed
스크린샷 2024-12-18 오후 8 39 05

both counts should be same because the count means "All Answers" and it gets from same DB, same Hibernate query.
the difference between those is only 'sorts.add(Sort.Order.desc("voter"));' and attribute voter is @manytomany.
It is more strange that when the Entity Answer gets only one voter attribute, it works perfectly(both count is same) but when it gets two or more voter attribute, it just returns 9, not all of All Answers.

Please Help
If my English is Bad, I'm sorry. If you need extra explanation, Please notify me.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 18, 2024
@christophstrobl
Copy link
Member

Thank you for getting in touch. It would be great if you could take the time to reduce the linked sample application to a minimum. So just the repository, a failing testcase and if possible something using plain JPA without the spring bits to see if it is a spring or a persistence provider problem. Ideally using Testcontainers so that we can unzip or git clone, build, and deploy the reproducer.

@christophstrobl christophstrobl added the status: waiting-for-feedback We need additional information before we can continue label Dec 18, 2024
@oungsi2000
Copy link
Author

oungsi2000 commented Dec 24, 2024

sorry for late reply, I was so busy..
I made minimum spring app testing JpaRepository.
when the parameter of sort.by is set instance of java.utils.Set, it goes wrong
please check

in the test code, method t4 is the checking method, and t1, t2, t3 is for generating database entity.
https://github.com/oungsi2000/JpaTest

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants