From 7c2d653b962555cee13246e856bf9f8a84a1ae27 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 31 May 2023 10:39:54 +0300 Subject: [PATCH] Fix native query support in Hibernate Reactive Fixes: #33713 --- .../deployment/HibernateReactiveProcessor.java | 8 ++++++++ .../it/hibernate/reactive/postgresql/GuineaPig.java | 2 ++ .../postgresql/HibernateReactiveTestEndpoint.java | 9 +++++++++ .../reactive/postgresql/HibernateReactiveTest.java | 10 ++++++++++ 4 files changed, 29 insertions(+) diff --git a/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java b/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java index ecbc0d26c5a58..330009c56a1a6 100644 --- a/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java +++ b/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java @@ -45,6 +45,7 @@ import io.quarkus.deployment.builditem.SystemPropertyBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; +import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem; import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem; import io.quarkus.deployment.recording.RecorderContext; import io.quarkus.hibernate.orm.deployment.HibernateConfigUtil; @@ -101,6 +102,13 @@ void reflections(BuildProducer reflectiveClass) { reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, REFLECTIVE_CONSTRUCTORS_NEEDED)); } + @BuildStep + void services(BuildProducer producer) { + producer.produce( + new ServiceProviderBuildItem(org.hibernate.service.spi.SessionFactoryServiceContributor.class.getName(), + org.hibernate.reactive.service.internal.ReactiveSessionFactoryServiceContributor.class.getName())); + } + @BuildStep @Record(STATIC_INIT) public void build(RecorderContext recorderContext, diff --git a/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/GuineaPig.java b/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/GuineaPig.java index 0ff88f16cb883..ff96db7b56109 100644 --- a/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/GuineaPig.java +++ b/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/GuineaPig.java @@ -4,10 +4,12 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; +import jakarta.persistence.NamedNativeQuery; import jakarta.persistence.Table; @Entity @Table(name = "Pig") +@NamedNativeQuery(name = "pig.all", query = "select * from Pig", resultClass = GuineaPig.class) public class GuineaPig { @Id diff --git a/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTestEndpoint.java b/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTestEndpoint.java index 17f8f9e5457a4..a72525d0cb3c0 100644 --- a/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTestEndpoint.java +++ b/integration-tests/hibernate-reactive-postgresql/src/main/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTestEndpoint.java @@ -1,5 +1,7 @@ package io.quarkus.it.hibernate.reactive.postgresql; +import java.util.List; + import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -25,6 +27,13 @@ public class HibernateReactiveTestEndpoint { @Inject PgPool pgPool; + @GET + @Path("/reactiveFindNativeQuery") + public Uni> reactiveFindNativeQuery() { + return populateDB() + .chain(() -> sessionFactory.withSession(s -> s.createNamedQuery("pig.all", GuineaPig.class).getResultList())); + } + @GET @Path("/reactiveFindMutiny") public Uni reactiveFindMutiny() { diff --git a/integration-tests/hibernate-reactive-postgresql/src/test/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTest.java b/integration-tests/hibernate-reactive-postgresql/src/test/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTest.java index ff08d1733d6bd..80f2a4a1b7d9a 100644 --- a/integration-tests/hibernate-reactive-postgresql/src/test/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTest.java +++ b/integration-tests/hibernate-reactive-postgresql/src/test/java/io/quarkus/it/hibernate/reactive/postgresql/HibernateReactiveTest.java @@ -1,5 +1,6 @@ package io.quarkus.it.hibernate.reactive.postgresql; +import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -18,6 +19,15 @@ @TestHTTPEndpoint(HibernateReactiveTestEndpoint.class) public class HibernateReactiveTest { + @Test + public void reactiveFindNativeQuery() { + RestAssured.given().when() + .auth().preemptive().basic("scott", "jb0ss") + .get("/reactiveFindNativeQuery") + .then() + .statusCode(anyOf(is(200), is(204))); + } + @Test public void reactiveCowPersist() { RestAssured.given().when()