Search code examples
javaspring-data-jpajpql

Use cast in order by clause JPQL


I have a JPQL query in which I want to use cast operator in the order by clause. The query works in SQL but the application gives the shown error at runtime. How to use cast in orderby clause in JPQL query? Thanks in advance.

@Query("SELECT a FROM tableA a where a.tableB.ID = :ID ORDER BY cast(col as unsigned)")

Error

Caused by: org.hibernate.QueryException: Could not resolve requested type for CAST : unsigned : unsigned [SELECT r FROM classname a where a.tableB.ID = :ID ORDER BY CAST(col as unsigned)]

Solution

  • I think unsigned is not supported. interger might be the closest.

    @Query("SELECT a FROM tableA a where a.tableB.ID = :ID ORDER BY cast(col as integer)")