From 66806d5ca8d24602ca4f597f42405fed798c5cd4 Mon Sep 17 00:00:00 2001 From: Max Rydahl Andersen Date: Sun, 26 Apr 2020 10:47:43 +0200 Subject: [PATCH] Support fror @Inject StatelessSession Why: * Sometimes you just want to do raw data access This change addreses the need by: * Add StatelessSession as a possible injection. --- .../orm/deployment/HibernateOrmProcessor.java | 2 + .../hibernate/orm/StatelessSessionTest.java | 49 +++++++++++++++++++ .../DefaultStatelessSessionProducer.java | 16 ++++++ .../runtime/JPAResourceReferenceProvider.java | 35 ++++++++++++- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/StatelessSessionTest.java create mode 100644 extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/DefaultStatelessSessionProducer.java diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java index 7f3ef790910cbd..22c6364a24d10f 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -95,6 +95,7 @@ import io.quarkus.hibernate.orm.deployment.integration.HibernateOrmIntegrationRuntimeConfiguredBuildItem; import io.quarkus.hibernate.orm.runtime.DefaultEntityManagerFactoryProducer; import io.quarkus.hibernate.orm.runtime.DefaultEntityManagerProducer; +import io.quarkus.hibernate.orm.runtime.DefaultStatelessSessionProducer; import io.quarkus.hibernate.orm.runtime.HibernateOrmRecorder; import io.quarkus.hibernate.orm.runtime.JPAConfig; import io.quarkus.hibernate.orm.runtime.JPAResourceReferenceProvider; @@ -421,6 +422,7 @@ void registerBeans(BuildProducer additionalBeans, Combi if (isUserDefinedProducerMissing(combinedIndex.getIndex(), PERSISTENCE_CONTEXT)) { additionalBeans.produce(new AdditionalBeanBuildItem(DefaultEntityManagerProducer.class)); } + additionalBeans.produce(new AdditionalBeanBuildItem(DefaultStatelessSessionProducer.class)); } } diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/StatelessSessionTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/StatelessSessionTest.java new file mode 100644 index 00000000000000..05e0cada29b3c9 --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/StatelessSessionTest.java @@ -0,0 +1,49 @@ +package io.quarkus.hibernate.orm; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.List; + +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.TransactionRequiredException; + +import org.hibernate.StatelessSession; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.Arc; +import io.quarkus.hibernate.orm.enhancer.Address; +import io.quarkus.test.QuarkusUnitTest; + +/** + * @author Max Rydahl Andersen + */ +public class StatelessSessionTest { + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClass(Address.class) + .addAsResource("application.properties")); + + @Inject + StatelessSession statelessSession; + + @Test + public void testStatelessSession() { + Arc.container().requestContext().activate(); + try { + List list = statelessSession.createNativeQuery("SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS") + .addScalar("VALUE") + .list(); + assertNotNull(list); + } finally { + Arc.container().requestContext().terminate(); + } + } + +} diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/DefaultStatelessSessionProducer.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/DefaultStatelessSessionProducer.java new file mode 100644 index 00000000000000..ad4975d2354f71 --- /dev/null +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/DefaultStatelessSessionProducer.java @@ -0,0 +1,16 @@ +package io.quarkus.hibernate.orm.runtime; + +import javax.enterprise.inject.Produces; +import javax.inject.Singleton; +import javax.persistence.PersistenceContext; + +import org.hibernate.StatelessSession; + +public class DefaultStatelessSessionProducer { + + @Produces + @PersistenceContext + @Singleton + StatelessSession statelessSession; + +} diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/JPAResourceReferenceProvider.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/JPAResourceReferenceProvider.java index 3d0a3142f3c0e8..b1b1af6042590e 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/JPAResourceReferenceProvider.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/JPAResourceReferenceProvider.java @@ -9,6 +9,8 @@ import javax.persistence.PersistenceContext; import javax.persistence.PersistenceUnit; +import org.hibernate.StatelessSession; + import io.quarkus.arc.Arc; import io.quarkus.arc.InstanceHandle; import io.quarkus.arc.ResourceReferenceProvider; @@ -26,6 +28,37 @@ public InstanceHandle get(Type type, Set annotations) { return () -> jpaConfig.getEntityManagerFactory(pu.unitName()); } } else if (EntityManager.class.equals(type)) { + PersistenceContext pc = getAnnotation(annotations, PersistenceContext.class); + if (pc != null) { + if (jpaConfig.isJtaEnabled()) { + TransactionEntityManagers transactionEntityManagers = Arc.container() + .instance(TransactionEntityManagers.class).get(); + ForwardingEntityManager entityManager = new ForwardingEntityManager() { + + @Override + protected EntityManager delegate() { + return transactionEntityManagers.getEntityManager(pc.unitName()); + } + }; + return () -> entityManager; + } else { + EntityManagerFactory entityManagerFactory = jpaConfig.getEntityManagerFactory(pc.unitName()); + EntityManager entityManager = entityManagerFactory.createEntityManager(); + return new InstanceHandle() { + + @Override + public Object get() { + return entityManager; + } + + @Override + public void destroy() { + entityManager.close(); + } + }; + } + } + } else if (StatelessSession.class.equals(type)) { PersistenceContext pc = getAnnotation(annotations, PersistenceContext.class); if (pc != null) { if (jpaConfig.isJtaEnabled()) { @@ -56,8 +89,8 @@ public void destroy() { }; } } - } + } return null; }