I have had to convert a couple of derived (keyword-based) queries into corresponding native ones (Postgres sql) and I have noticed the ORDER BY clause does not seem to work. Here are two sample queries:
@Query(nativeQuery = true, value =
"SELECT * from entityOne a LEFT OUTER JOIN entityTwo b on a.field1= b.id " +
"WHERE b.field2 = 0 AND b.field3 is null ORDER BY b.field4 DESC")
fun findAllByField3NullAndField2OrderByField4Desc(
pageable: Pageable
): Page<MyEntity>
And
@Query(nativeQuery = true, value =
"SELECT * from entityOne a LEFT OUTER JOIN entityTwo b on a.field1= b.id " +
"WHERE b.field2 = <> 4 AND b.field3 = ?1 ORDER BY b.field4 DESC")
fun findAllByField3AndField2OrderByField4Desc(
queue: UUID,
pageable: Pageable
): Page<MyEntity>
After a number of tests I came to the conclusion that native queries do not seem to handle well ORDER BY clauses and this causes an issue with my current application. Is there any way to handle them with native queries? Any potential alternative? I would also be happy with converting them back to derived queries.
Thank you
question from:
https://stackoverflow.com/questions/65649099/converting-derived-jpa-query-into-native-query-order-by-is-not-working 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…