Skip to content

Commit 543e632

Browse files
authored
Merge pull request #31419 from bakrhaso/31210
Handle text blocks when creating Panache find query
2 parents 8312e06 + d0d60cb commit 543e632

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

extensions/panache/panache-hibernate-common/runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/PanacheJpaUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static String createFindQuery(Class<?> entityClass, String query, int par
6161
return "FROM " + getEntityName(entityClass);
6262
}
6363

64-
String trimmed = query.trim();
64+
String trimmed = query.replace('\n', ' ').replace('\r', ' ').trim();
6565
if (trimmed.isEmpty()) {
6666
return "FROM " + getEntityName(entityClass);
6767
}

integration-tests/hibernate-orm-panache-kotlin/src/main/kotlin/io/quarkus/it/panache/kotlin/TestEndpoint.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.quarkus.panache.common.Page
55
import io.quarkus.panache.common.Parameters
66
import io.quarkus.panache.common.Sort
77
import io.quarkus.panache.common.exception.PanacheQueryException
8+
import io.quarkus.runtime.annotations.RegisterForReflection
89
import jakarta.inject.Inject
910
import jakarta.persistence.LockModeType
1011
import jakarta.persistence.NoResultException
@@ -1091,4 +1092,47 @@ class TestEndpoint {
10911092

10921093
return "OK"
10931094
}
1095+
1096+
@GET
1097+
@Path("project")
1098+
@Transactional
1099+
fun testProject(): String {
1100+
@RegisterForReflection
1101+
data class MyProjection(
1102+
val projectedName: String
1103+
)
1104+
1105+
val mark = Person()
1106+
mark.name = "Mark"
1107+
mark.persistAndFlush()
1108+
1109+
val hqlWithoutSpace = """
1110+
select
1111+
name as projectedName
1112+
from
1113+
io.quarkus.it.panache.kotlin.Person
1114+
where
1115+
name = ?1
1116+
""".trimIndent()
1117+
val withoutSpace = Person.find(hqlWithoutSpace, "Mark").project(MyProjection::class.java).firstResult()
1118+
Assertions.assertNotNull(withoutSpace)
1119+
Assertions.assertEquals(mark.name, withoutSpace?.projectedName)
1120+
1121+
// There is a space behind "select "
1122+
val hqlWithSpace = """
1123+
select
1124+
name as projectedName
1125+
from
1126+
io.quarkus.it.panache.kotlin.Person
1127+
where
1128+
name = ?1
1129+
""".trimIndent()
1130+
val withSpace = Person.find(hqlWithSpace, "Mark").project(MyProjection::class.java).firstResult()
1131+
Assertions.assertNotNull(withSpace)
1132+
Assertions.assertEquals(mark.name, withSpace?.projectedName)
1133+
1134+
Person.deleteAll()
1135+
1136+
return "OK"
1137+
}
10941138
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.quarkus.it.panache
2+
3+
import io.quarkus.test.junit.QuarkusIntegrationTest
4+
import io.quarkus.test.junit.QuarkusTest
5+
import io.restassured.RestAssured
6+
import org.hamcrest.Matchers
7+
import org.junit.jupiter.api.Test
8+
9+
// Native tests
10+
@QuarkusIntegrationTest
11+
class ProjectionIT : ProjectionTest()
12+
13+
// Quarkus/JVM tests
14+
@QuarkusTest
15+
open class ProjectionTest {
16+
17+
@Test
18+
fun testProject() {
19+
RestAssured.`when`()["/test/project"].then().body(Matchers.`is`("OK"))
20+
}
21+
}

integration-tests/java-17/src/main/java/io/quarkus/it/hibernate/panache/person/PersonResource.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkus.it.hibernate.panache.person;
22

33
import java.net.URI;
4+
import java.util.Arrays;
45
import java.util.List;
56

67
import jakarta.transaction.Transactional;
@@ -24,4 +25,39 @@ public Response addPerson(Person person) {
2425
return Response.created(URI.create("/persons/entity/" + id)).build();
2526
}
2627

28+
29+
@GET
30+
@Path("hql-project")
31+
@Transactional
32+
public Response testPanacheHqlProject() {
33+
var mark = new Person();
34+
mark.firstname = "Mark";
35+
mark.lastname = "Mark";
36+
mark.persistAndFlush();
37+
38+
var hqlWithoutSpace = """
39+
select
40+
firstname,
41+
lastname
42+
from
43+
io.quarkus.it.hibernate.panache.person.Person
44+
where
45+
firstname = ?1
46+
""";
47+
var persistedWithoutSpace = Person.find(hqlWithoutSpace, "Mark").project(PersonName.class).firstResult();
48+
49+
// We need to escape the whitespace in Java otherwise the compiler removes it.
50+
var hqlWithSpace = """
51+
select\s
52+
firstname,
53+
lastname
54+
from
55+
io.quarkus.it.hibernate.panache.person.Person
56+
where
57+
firstname = ?1
58+
""";
59+
var persistedWithSpace = Person.find(hqlWithSpace, "Mark").project(PersonName.class).firstResult();
60+
61+
return Response.ok(Arrays.asList(persistedWithoutSpace, persistedWithSpace)).build();
62+
}
2763
}

integration-tests/java-17/src/test/java/io/quarkus/it/hibernate/panache/person/PersonResourceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static io.restassured.RestAssured.when;
55
import static org.hamcrest.CoreMatchers.is;
66

7+
import io.quarkus.test.TestTransaction;
78
import org.junit.jupiter.api.Test;
89

910
import io.quarkus.it.mongodb.panache.person.Person;
@@ -19,6 +20,7 @@ class PersonResourceTest {
1920
private static final String ROOT_URL = "/hibernate/persons";
2021

2122
@Test
23+
@TestTransaction
2224
void testRecordInPanache() {
2325
var person1 = new Person();
2426
person1.firstname = "Loïc";
@@ -42,4 +44,11 @@ void testRecordInPanache() {
4244
.body("size()", is(2));
4345
}
4446

47+
@Test
48+
@TestTransaction
49+
void testHqlPanacheProject() {
50+
when().get(ROOT_URL + "/hql-project")
51+
.then().statusCode(200)
52+
.body("size()", is(2));
53+
}
4554
}

0 commit comments

Comments
 (0)