From fc558a09f3f3d836b0738c27ba1a944b49e78541 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 26 Jan 2023 15:04:34 -0300 Subject: [PATCH] Support StatementInspector as `@PersistenceUnitExtension` managed bean - Fixes #27403 --- .../hibernate/orm/deployment/ClassNames.java | 1 + .../deployment/HibernateOrmCdiProcessor.java | 3 +- ...plicationScopedStatementInspectorTest.java | 92 +++++++++++++++++++ .../FastBootEntityManagerFactoryBuilder.java | 7 ++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/jdbc/ApplicationScopedStatementInspectorTest.java diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java index 644689095d286..effe1a6823ba6 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java @@ -55,5 +55,6 @@ private static DotName createConstant(String fqcn) { public static final DotName SESSION = createConstant("org.hibernate.Session"); public static final DotName INTERCEPTOR = createConstant("org.hibernate.Interceptor"); + public static final DotName STATEMENT_INSPECTOR = createConstant("org.hibernate.resource.jdbc.spi.StatementInspector"); } diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java index 18d19e56a3147..38bcc201eed4d 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java @@ -46,7 +46,8 @@ public class HibernateOrmCdiProcessor { private static final Set PERSISTENCE_UNIT_EXTENSION_VALID_TYPES = Set.of( ClassNames.TENANT_RESOLVER, ClassNames.TENANT_CONNECTION_RESOLVER, - ClassNames.INTERCEPTOR); + ClassNames.INTERCEPTOR, + ClassNames.STATEMENT_INSPECTOR); @BuildStep AnnotationsTransformerBuildItem convertJpaResourceAnnotationsToQualifier( diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/jdbc/ApplicationScopedStatementInspectorTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/jdbc/ApplicationScopedStatementInspectorTest.java new file mode 100644 index 0000000000000..81881654bc0db --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/jdbc/ApplicationScopedStatementInspectorTest.java @@ -0,0 +1,92 @@ +package io.quarkus.hibernate.orm.jdbc; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; + +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.transaction.UserTransaction; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.resource.jdbc.spi.StatementInspector; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.hibernate.orm.PersistenceUnitExtension; +import io.quarkus.runtime.StartupEvent; +import io.quarkus.test.QuarkusUnitTest; + +public class ApplicationScopedStatementInspectorTest { + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar + .addClass(MyEntity.class) + .addClass(ApplicationStatementInspector.class)) + .withConfigurationResource("application.properties"); + + @Inject + SessionFactory sessionFactory; + + @Inject + Session session; + + @Inject + UserTransaction transaction; + + public void initData(@Observes StartupEvent event) throws Exception { + transaction.begin(); + for (int i = 0; i < 3; i++) { + MyEntity entity = new MyEntity(i); + session.persist(entity); + } + transaction.commit(); + } + + @BeforeEach + public void clearStatementInspector() { + ApplicationStatementInspector.statements.clear(); + } + + @Test + public void testStatementInspectorIsLoaded() throws Exception { + transaction.begin(); + session.find(MyEntity.class, 0); + transaction.commit(); + assertThat(ApplicationStatementInspector.statements).hasSize(1); + } + + @Entity(name = "myentity") + @Table + public static class MyEntity { + + @Id + public Integer id; + + public MyEntity() { + } + + public MyEntity(int id) { + this.id = id; + } + } + + @PersistenceUnitExtension // @ApplicationScoped is the default + public static class ApplicationStatementInspector implements StatementInspector { + + static List statements = new ArrayList<>(); + + @Override + public String inspect(String sql) { + statements.add(sql); + return sql; + } + } +} diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootEntityManagerFactoryBuilder.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootEntityManagerFactoryBuilder.java index cad28dcac61a9..f634a1f889564 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootEntityManagerFactoryBuilder.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootEntityManagerFactoryBuilder.java @@ -23,6 +23,7 @@ import org.hibernate.internal.SessionFactoryImpl; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.proxy.EntityNotFoundDelegate; +import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.tool.schema.spi.CommandAcceptanceException; @@ -184,6 +185,12 @@ protected void populate(String persistenceUnitName, SessionFactoryOptionsBuilder if (!interceptorInstance.isUnsatisfied()) { options.applyStatelessInterceptorSupplier(interceptorInstance::get); } + + InjectableInstance statementInspectorInstance = PersistenceUnitUtil + .singleExtensionInstanceForPersistenceUnit(StatementInspector.class, persistenceUnitName); + if (!statementInspectorInstance.isUnsatisfied()) { + options.applyStatementInspector(statementInspectorInstance.get()); + } } private static class ServiceRegistryCloser implements SessionFactoryObserver {