Hibernate Sorting does not work on oracle db 19c

Hi,

I am using Hibernate to do pagination and sorting from Oracle DB. While pagination works sorting does not work. Given below is the query that hibernate is generating whereas this the query that i want.

*2025-06-29 13:54:16.425[0;39m [32mDEBUG[0;39m [35m19372[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36morg.hibernate.SQL                       [0;39m [2m:[0;39m **select tutorial0_.tutorial_id as tutorial_id1_0_, tutorial0_.description as description2_0_, tutorial0_.levels as levels3_0_, tutorial0_.title as title4_0_ from tutorial tutorial0_ offset ? rows fetch next ? rows only***
*Hibernate: **select tutorial0_.tutorial_id as tutorial_id1_0_, tutorial0_.description as description2_0_, tutorial0_.levels as levels3_0_, tutorial0_.title as title4_0_ from tutorial tutorial0_ offset ? rows fetch next ? rows only***
*[2m2025-06-29 13:54:16.433[0;39m [32mDEBUG[0;39m [35m19372[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36morg.hibernate.SQL                       [0;39m [2m:[0;39m select count(tutorial0_.tutorial_id) as col_0_0_ from tutorial tutorial0_*
*Hibernate: select count(tutorial0_.tutorial_id) as col_0_0_ from tutorial tutorial0_*
*[2m2025-06-29 13:54:16.436[0;39m [32mDEBUG[0;39m [35m19372[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.orm.jpa.JpaTransactionManager       [0;39m [2m:[0;39m Initiating transaction commit*

But the query that i want is this query, which works in oracle.
***select * from tutorial **order by tutorial_id desc** OFFSET 15 ROWS FETCH NEXT 5 ROWS ONLY;***

Please NOTE order by tutorial_id desc is missing in the Hibernate generated query

Thanks in advance for your help.

Please share the HQL/Criteria query that you are using and how you are executing it. Also, which version of Hibernate ORM are you using?

Thanks for your response beikov.
I am using Hibernate Version :5.6.12.Final.

This is the criteria query hibernate is generating for pagination not sorting.

Hibernate: select tutorial0_.tutorial_id as tutorial_id1_0_, tutorial0_.description as description2_0_, tutorial0_.levels as levels3_0_, tutorial0_.title as title4_0_ from tutorial tutorial0_ offset ? rows fetch next ? rows only
[2m2025-06-29 13:54:16.433[0;39m [32mDEBUG[0;39m [35m19372[0;39m [2m—[0;39m [2m[nio-8080-exec-1][0;39m [36morg.hibernate.SQL [0;39m [2m:[0;39m select count(tutorial0_.tutorial_id) as col_0_0_ from tutorial tutorial0_

Here is the part of the controller class for the pageable with and without sorting.
I am using findAll method to get the data for both the situations(paging and sorting).

The first commented line FirstNextPage works fine no sorting but pagination.
//Pageable FirstNextPage = PageRequest.of((page-1), size);

The second line FirstNextPage has sorting process which fails to bring any data.
Pageable FirstNextPage = PageRequest.of((page-1), size, Sort.by(Sort.Direction.ASC, “tutorial_id”));

pageTuts = tutorialRepository.findAll(FirstNextPage);
tutorials = pageTuts.getContent();

Here is the part of the logs after using the sorting process from the second line.
[0;39m [2m:[0;39m Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@573d63a2]
[2m2025-06-30 18:12:32.841[0;39m [32mDEBUG[0;39m [35m27008[0;39m [2m—[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.beans.CachedIntrospectionResults [0;39m [2m:[0;39m Not strongly caching class [com.bezkoder.spring.thymeleaf.entity.Tutorial] because it is not cache-safe
[2m2025-06-30 18:12:32.844[0;39m [32mDEBUG[0;39m [35m27008[0;39m [2m—[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.orm.jpa.JpaTransactionManager [0;39m [2m:[0;39m Initiating transaction rollback

Sorry, but we can’t help you with Spring problems. Ask on a Spring forum instead. This is a forum for Hibernate ORM, so unless you are using Hibernate ORM APIs, you won’t get an answer here.