Repository: quarkusio/quarkus
Issue: Hibernate Panache sorting not working with Named Query

### Describe the bug

Sorting not working with Named Query

```
@Entity
@NamedQueries({
        @NamedQuery(name = "countFruit",
                query = "FROM Fruit")
})
public class Fruit extends PanacheEntity {

    @Column(length = 40, unique = true)
    public String name;

    public static List<Fruit> listSortedFruitsByNamedQuery() {
        return list("#countFruit", Sort.by("name").ascending(), new HashMap<>()); // Not sorting
    }

    public static List<Fruit> listSortedFruits() {
        return list("FROM Fruit", Sort.by("name").ascending(), new HashMap<>()); //Sorting is done
    }

}
```

### Expected behavior

find, list and stream functions should support sorting for named queries also. 

### Actual behavior

No errors, but the result list is not sorted if named query is used 

### How to Reproduce?

_No response_

### Output of `uname -a` or `ver`

_No response_

### Output of `java -version`

openjdk version "11.0.11"

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

2.3.0.FINAL

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

Apache Maven 3.6.3

### Additional information

Possible related issue: https://github.com/quarkusio/quarkus/issues/20758

Changed files:
extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java
extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java
integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java
integration-tests/hibernate-reactive-panache/src/main/java/io/quarkus/it/panache/reactive/TestEndpoint.java

diff --git a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java
index 63f987a9f7e..717e433a4d7 100644
--- a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java
+++ b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java
@@ -196,9 +196,13 @@ public PanacheQueryType find(Class<?> entityClass, String query, Object... param
     public PanacheQueryType find(Class<?> entityClass, String query, Sort sort, Object... params) {
         String findQuery = PanacheJpaUtil.createFindQuery(entityClass, query, paramCount(params));
         EntityManager em = getEntityManager(entityClass);
-        // FIXME: check for duplicate ORDER BY clause?
         if (PanacheJpaUtil.isNamedQuery(query)) {
             String namedQuery = query.substring(1);
+            if (sort != null) {
+                throw new IllegalArgumentException(
+                        "Sort cannot be used with named query, add an \\"order by\\" clause to the named query \\"" + namedQuery
+                                + "\\" instead");
+            }
             NamedQueryUtil.checkNamedQuery(entityClass, namedQuery);
             return createPanacheQuery(em, query, PanacheJpaUtil.toOrderBy(sort), params);
         }
@@ -212,9 +216,13 @@ public PanacheQueryType find(Class<?> entityClass, String query, Map<String, Obj
     public PanacheQueryType find(Class<?> entityClass, String query, Sort sort, Map<String, Object> params) {
         String findQuery = PanacheJpaUtil.createFindQuery(entityClass, query, paramCount(params));
         EntityManager em = getEntityManager(entityClass);
-        // FIXME: check for duplicate ORDER BY clause?
         if (PanacheJpaUtil.isNamedQuery(query)) {
             String namedQuery = query.substring(1);
+            if (sort != null) {
+                throw new IllegalArgumentException(
+                        "Sort cannot be used with named query, add an \\"order by\\" clause to the named query \\"" + namedQuery
+                                + "\\" instead");
+            }
             NamedQueryUtil.checkNamedQuery(entityClass, namedQuery);
             return createPanacheQuery(em, query, PanacheJpaUtil.toOrderBy(sort), params);
         }
diff --git a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java
index 03e920604d2..b8c6e53508f 100644
--- a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java
+++ b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/AbstractJpaOperations.java
@@ -192,9 +192,13 @@ public PanacheQueryType find(Class<?> entityClass, String query, Object... param
     public PanacheQueryType find(Class<?> entityClass, String query, Sort sort, Object... params) {
         String findQuery = PanacheJpaUtil.createFindQuery(entityClass, query, paramCount(params));
         Uni<Mutiny.Session> session = getSession();
-        // FIXME: check for duplicate ORDER BY clause?
         if (PanacheJpaUtil.isNamedQuery(query)) {
             String namedQuery = query.substring(1);
+            if (sort != null) {
+                throw new IllegalArgumentException(
+                        "Sort cannot be used with named query, add an \\"order by\\" clause to the named query \\"" + namedQuery
+                                + "\\" instead");
+            }
             NamedQueryUtil.checkNamedQuery(entityClass, namedQuery);
             return createPanacheQuery(session, query, PanacheJpaUtil.toOrderBy(sort), params);
         }
@@ -208,9 +212,13 @@ public PanacheQueryType find(Class<?> entityClass, String query, Map<String, Obj
     public PanacheQueryType find(Class<?> entityClass, String query, Sort sort, Map<String, Object> params) {
         String findQuery = PanacheJpaUtil.createFindQuery(entityClass, query, paramCount(params));
         Uni<Mutiny.Session> session = getSession();
-        // FIXME: check for duplicate ORDER BY clause?
         if (PanacheJpaUtil.isNamedQuery(query)) {
             String namedQuery = query.substring(1);
+            if (sort != null) {
+                throw new IllegalArgumentException(
+                        "Sort cannot be used with named query, add an \\"order by\\" clause to the named query \\"" + namedQuery
+                                + "\\" instead");
+            }
             NamedQueryUtil.checkNamedQuery(entityClass, namedQuery);
             return createPanacheQuery(session, query, PanacheJpaUtil.toOrderBy(sort), params);
         }
diff --git a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java
index 1569e415ae2..2daf1aa31eb 100644
--- a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java
+++ b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java
@@ -170,6 +170,8 @@ public String testModel() {
         Assertions.assertEquals(person, persons.get(0));
         Assertions.assertEquals(1, Person.find("#Person.getByName", Parameters.with("name", "stef")).count());
         Assertions.assertThrows(PanacheQueryException.class, () -> Person.find("#Person.namedQueryNotFound").list());
+        Assertions.assertThrows(IllegalArgumentException.class,
+                () -> Person.find("#Person.getByName", Sort.by("name"), Parameters.with("name", "stef")));
         NamedQueryEntity.find("#NamedQueryMappedSuperClass.getAll").list();
         NamedQueryEntity.find("#NamedQueryEntity.getAll").list();
         NamedQueryWith2QueriesEntity.find("#NamedQueryWith2QueriesEntity.getAll1").list();
@@ -711,6 +713,8 @@ public String testModelDao() {
         Assertions.assertEquals(1, persons.size());
         Assertions.assertEquals(person, persons.get(0));
         Assertions.assertThrows(PanacheQueryException.class, () -> personDao.find("#Person.namedQueryNotFound").list());
+        Assertions.assertThrows(IllegalArgumentException.class,
+                () -> personDao.find("#Person.getByName", Sort.by("name"), Parameters.with("name", "stef")));
         namedQueryRepository.find("#NamedQueryMappedSuperClass.getAll").list();
         namedQueryRepository.find("#NamedQueryEntity.getAll").list();
         namedQueryWith2QueriesRepository.find("#NamedQueryWith2QueriesEntity.getAll1").list();
diff --git a/integration-tests/hibernate-reactive-panache/src/main/java/io/quarkus/it/panache/reactive/TestEndpoint.java b/integration-tests/hibernate-reactive-panache/src/main/java/io/quarkus/it/panache/reactive/TestEndpoint.java
index aefb180316f..4a9ca0cb80f 100644
--- a/integration-tests/hibernate-reactive-panache/src/main/java/io/quarkus/it/panache/reactive/TestEndpoint.java
+++ b/integration-tests/hibernate-reactive-panache/src/main/java/io/quarkus/it/panache/reactive/TestEndpoint.java
@@ -224,7 +224,10 @@ public Uni<String> testModel() {
                                 return assertThrows(PanacheQueryException.class,
                                         () -> Person.find("#Person.namedQueryNotFound").list(),
                                         "singleResult should have thrown");
-                            }).flatMap(v -> NamedQueryEntity.list("#NamedQueryMappedSuperClass.getAll"))
+                            }).flatMap(v -> assertThrows(IllegalArgumentException.class,
+                                    () -> Person.list("#Person.getByName", Sort.by("name"), Parameters.with("name", "stef")),
+                                    "Should have thrown sort exception"))
+                            .flatMap(v -> NamedQueryEntity.list("#NamedQueryMappedSuperClass.getAll"))
                             .flatMap(v -> NamedQueryEntity.list("#NamedQueryEntity.getAll"))
                             .flatMap(v -> NamedQueryWith2QueriesEntity.list("#NamedQueryWith2QueriesEntity.getAll1"))
                             .flatMap(v -> NamedQueryWith2QueriesEntity.list("#NamedQueryWith2QueriesEntity.getAll2"))
@@ -966,7 +969,10 @@ public Uni<String> testModelDao() {
                                 return assertThrows(PanacheQueryException.class,
                                         () -> personDao.find("#Person.namedQueryNotFound").list(),
                                         "singleResult should have thrown");
-                            }).flatMap(v -> namedQueryRepository.list("#NamedQueryMappedSuperClass.getAll"))
+                            }).flatMap(v -> assertThrows(IllegalArgumentException.class,
+                                    () -> personDao.list("#Person.getByName", Sort.by("name"), Parameters.with("name", "stef")),
+                                    "Should have thrown sort exception"))
+                            .flatMap(v -> namedQueryRepository.list("#NamedQueryMappedSuperClass.getAll"))
                             .flatMap(v -> namedQueryRepository.list("#NamedQueryEntity.getAll"))
                             .flatMap(v -> namedQueryWith2QueriesRepository.list("#NamedQueryWith2QueriesEntity.getAll1"))
                             .flatMap(v -> namedQueryWith2QueriesRepository.list("#NamedQueryWith2QueriesEntity.getAll2"))