Skip to content

Commit

Permalink
Support fror @Inject StatelessSession
Browse files Browse the repository at this point in the history
Why:

 * Sometimes you just want to do raw data access

This change addreses the need by:

 * Add StatelessSession as a possible injection.
  • Loading branch information
maxandersen committed Apr 26, 2020
1 parent 6167fae commit 66806d5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -421,6 +422,7 @@ void registerBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans, Combi
if (isUserDefinedProducerMissing(combinedIndex.getIndex(), PERSISTENCE_CONTEXT)) {
additionalBeans.produce(new AdditionalBeanBuildItem(DefaultEntityManagerProducer.class));
}
additionalBeans.produce(new AdditionalBeanBuildItem(DefaultStatelessSessionProducer.class));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <manderse@redhat.com>
*/
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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +28,37 @@ public InstanceHandle<Object> get(Type type, Set<Annotation> 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<Object>() {

@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()) {
Expand Down Expand Up @@ -56,8 +89,8 @@ public void destroy() {
};
}
}
}

}
return null;
}

Expand Down

0 comments on commit 66806d5

Please sign in to comment.