Repository: quarkusio/quarkus
Issue: Panache project() is not working if used with distinct + case-sensitive HQL query

### Describe the bug

For an entity that has getter defined with camelCase attribute name ie: `myId` executing query:
```java
Data.find("SELECT DISTINCT d.myId FROM Data d", Sort.by("myId"))
        .project(Long.class)
        .list();
```
will result in:
```
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: myid of: de.turing85.panache.projection.entity.Data [select distinct new java.lang.Long(d.myid) FROM de.turing85.panache.projection.entity.Data d ORDER BY myId]
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:757)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:114)
        at io.quarkus.hibernate.orm.runtime.session.TransactionScopedSession.createQuery(TransactionScopedSession.java:349)
        at org.hibernate.engine.spi.SessionLazyDelegator.createQuery(SessionLazyDelegator.java:547)
```
The issue is probably here: https://github.com/quarkusio/quarkus/blob/8e4ee686b538e8cb7cbb402ef084ef0925fb83cf/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java#L104


### Expected behavior

Distinct query should work with HQL compliant query.

### Actual behavior

It does not work, an exception is raised.

```
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: myid of: de.turing85.panache.projection.entity.Data [select distinct new java.lang.Long(d.myid) FROM de.turing85.panache.projection.entity.Data d ORDER BY myId]
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:757)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:114)
        at io.quarkus.hibernate.orm.runtime.session.TransactionScopedSession.createQuery(TransactionScopedSession.java:349)
        at org.hibernate.engine.spi.SessionLazyDelegator.createQuery(SessionLazyDelegator.java:547)
```

### How to Reproduce?

https://github.com/turing85/quarkus-panache-projection/tree/reanme-id-to-myId

Check this line: https://github.com/turing85/quarkus-panache-projection/blob/reanme-id-to-myId/src/main/java/de/turing85/panache/projection/DataResource.java#L36

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

Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000

### Output of `java -version`

openjdk version "19" 2022-09-20 OpenJDK Runtime Environment Homebrew (build 19) OpenJDK 64-Bit Server VM Homebrew (build 19, mixed mode, sharing)

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

2.13.3.Final

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

Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Maven home: /Users/jasminsuljic/.m2/wrapper/dists/apache-maven-3.8.4-bin/52ccbt68d252mdldqsfsn03jlf/apache-maven-3.8.4 Java version: 19, vendor: Homebrew, runtime: /usr/local/Cellar/openjdk/19/libexec/openjdk.jdk/Contents/Home Default locale: en_GB, platform encoding: UTF-8 OS name: "mac os x", version: "12.6", arch: "x86_64", family: "mac"

### Additional information

_No response_

Changed files:
extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/CommonPanacheQueryImpl.java
extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.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/CommonPanacheQueryImpl.java b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java
index 894357ec8a0..70bfb1a8033 100644
--- a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java
+++ b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java
@@ -95,15 +95,14 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
             int endSelect = lowerCasedTrimmedQuery.indexOf(" from ");
             String trimmedQuery = query.trim();
             // 7 is the length of "select "
-            String selectClause = trimmedQuery.substring(7, endSelect);
+            String selectClause = trimmedQuery.substring(7, endSelect).trim();
             String from = trimmedQuery.substring(endSelect);
             StringBuilder newQuery = new StringBuilder("select ");
             // Handle select-distinct. HQL example: select distinct new org.acme.ProjectionClass...
-            String lowerCasedTrimmedSelect = selectClause.trim().toLowerCase();
-            boolean distinctQuery = lowerCasedTrimmedSelect.startsWith("distinct ");
+            boolean distinctQuery = selectClause.toLowerCase().startsWith("distinct ");
             if (distinctQuery) {
                 // 9 is the length of "distinct "
-                selectClause = lowerCasedTrimmedSelect.substring(9).trim();
+                selectClause = selectClause.substring(9).trim();
                 newQuery.append("distinct ");
             }
 
diff --git a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/CommonPanacheQueryImpl.java b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/CommonPanacheQueryImpl.java
index 3623f2ee25c..96896088c52 100644
--- a/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/CommonPanacheQueryImpl.java
+++ b/extensions/panache/hibernate-reactive-panache-common/runtime/src/main/java/io/quarkus/hibernate/reactive/panache/common/runtime/CommonPanacheQueryImpl.java
@@ -82,15 +82,14 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
             int endSelect = lowerCasedTrimmedQuery.indexOf(" from ");
             String trimmedQuery = query.trim();
             // 7 is the length of "select "
-            String selectClause = trimmedQuery.substring(7, endSelect);
+            String selectClause = trimmedQuery.substring(7, endSelect).trim();
             String from = trimmedQuery.substring(endSelect);
             StringBuilder newQuery = new StringBuilder("select ");
             // Handle select-distinct. HQL example: select distinct new org.acme.ProjectionClass...
-            String lowerCasedTrimmedSelect = selectClause.trim().toLowerCase();
-            boolean distinctQuery = lowerCasedTrimmedSelect.startsWith("distinct ");
+            boolean distinctQuery = selectClause.toLowerCase().startsWith("distinct ");
             if (distinctQuery) {
                 // 9 is the length of "distinct "
-                selectClause = lowerCasedTrimmedSelect.substring(9).trim();
+                selectClause = selectClause.substring(9).trim();
                 newQuery.append("distinct ");
             }
 
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 2641e034de2..363fd589281 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
@@ -1253,6 +1253,18 @@ public String testProjection() {
         long countDistinct = projectionDistinctQuery.count();
         Assertions.assertEquals(1L, countDistinct);
 
+        // We are checking that not everything gets lowercased
+        PanacheQuery<CatProjectionBean> letterCaseQuery = Cat
+                // The spaces at the beginning are intentional
+                .find("   SELECT   disTINct  'GARFIELD', 'JoN ArBuCkLe' from Cat c where name = :NamE group by name  ",
+                        Parameters.with("NamE", bubulle.name))
+                .project(CatProjectionBean.class);
+
+        CatProjectionBean catView = letterCaseQuery.firstResult();
+        // Must keep the letter case
+        Assertions.assertEquals("GARFIELD", catView.getName());
+        Assertions.assertEquals("JoN ArBuCkLe", catView.getOwnerName());
+
         Cat.deleteAll();
         CatOwner.deleteAll();
 
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 fb50b9a4287..7046f84404e 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
@@ -1675,6 +1675,16 @@ public Uni<String> testProjection2() {
                     Assertions.assertTrue(
                             exception.getMessage().startsWith("Unable to perform a projection on a 'select new' query"));
                 })
+                .chain(() -> Cat
+                        .find("   SELECT   disTINct  'GARFIELD', 'JoN ArBuCkLe' from Cat c where name = :NamE group by name  ",
+                                Parameters.with("NamE", catName))
+                        .project(CatProjectionBean.class)
+                        .<CatProjectionBean> firstResult())
+                .invoke(catView -> {
+                    // Must keep the letter case
+                    Assertions.assertEquals("GARFIELD", catView.name);
+                    Assertions.assertEquals("JoN ArBuCkLe", catView.ownerName);
+                })
                 .replaceWith("OK");
     }