Assuming you have a record with some kind of id and what to know at what position it appears in your table when the table is ordered by some criteria you can do this with analytic functions. From this value you can trivially calculate the page it appears on, given a page size.
Example Schema (MySQL v8.0)
create table example (
id integer not null primary key,
text varchar(20));
insert into example(id, text) values (23,"Alfred");
insert into example(id, text) values (47,"Berta");
insert into example(id, text) values (11,"Carl");
insert into example(id, text) values (42,"Diana");
insert into example(id, text) values (17,"Ephraim");
insert into example(id, text) values (1,"Fiona");
insert into example(id, text) values (3,"Gerald");
Query
select *
from (
select id, text,
count(*) over (order by text) cnt // change the order by according to your needs
from example
// a where clause here limits the table before counting
) x where id = 42; // this where clause finds the row you are interested in
| id | text | cnt |
| --- | ----- | --- |
| 42 | Diana | 4 |
In order to use it with Spring Data JPA you put this query in a @Query
annotation and mark it as a native query.