mengmeng
mengmeng

Reputation: 1506

Use ORDER BY LENGTH query in JPA

Is there a way to use the SQL 'ORDER BY LENGTH` in JPA (Spring)? We're trying to eliminate the usage of native queries.

Here is the native query:

SELECT * FROM table WHERE columnOne ORDER BY LENGTH(columnTwo) DESC LIMIT 1

Thank you!

Upvotes: 1

Views: 547

Answers (1)

Under_Koen
Under_Koen

Reputation: 1078

To call functions in JPQL you use:

function(funtion_name, arg_1, arg_2, arg_n)

So your query would become:

select t from table t where t.columnOne order by function('length', t.columnTwo) desc

With JPQL you can't use limit in the query so you could use Pagination.

Upvotes: 2

Related Questions