Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

spring boot - Converting Derived JPA query into native query: ORDER BY is NOT working

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...