I have a backend application made in spring boot, and I want to load in front end, comments about some topic, and I load these comments paginated and sorted, it works fine if user just scrolls, and get the next page, but there is a problem, users can post comments as well, and these comments go in the table, from which we get paginated comments to send to front end. The problem is that if I get two pages for example with 10 comments each, and then post a comment that will go let's say somewhere between elements of the first page, because comments are sorted, then all items after the inserted comment are shifted by one position, and so the first comment of the third page will be the last element of the second page that was already sent to front end, and I get duplicate comments.
Pageable pageable = PageRequest.of(page, 8, Sort.by("text").ascending());
List<Comment> commentsPage = commentsRepository.findAllByBookId(bookId, pageable);
@Repository
public interface CommentsRepository extends JpaRepository<Comment, String> {
List<Comment> findAllByBookId(String bookId, Pageable pageable);
}
To solve this I was thinking of memorizing in the front end how many comments are added, and when I ask the next page to specify them to skip that number of comments, but I don't know how to do that.
I'm new to java and spring boot, if there are other solutions how I could solve this I'm open to suggestions.