fix(manager-api): featured APIs throwing an NPE when empty
MODIFY
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/AbstractJpaStorage.java
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/AbstractJpaStorage.java
@@ -85,9 +85,7 @@ public abstract class AbstractJpaStorage {
* @return the thread's entity manager
*/
public EntityManager getActiveEntityManager() {
- EntityManager em = emf.getEntityManager();
- // System.out.println("em(jpa)=" + em.hashCode());
- return em;
+ return emf.getEntityManager();
}
public Session getSession() {

MODIFY
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/EntityManagerFactoryAccessor.java
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/EntityManagerFactoryAccessor.java
@@ -29,7 +29,7 @@ import javax.persistence.PersistenceContext;
import org.hibernate.jpa.HibernatePersistenceProvider;
/**
- * Produces an instance of {@link EntityManagerFactory}.
+ * Produces an instance of {@link EntityManagerFactory}. Or the managed persistence unit, if there is one :-).
*
* @author eric.wittmann@redhat.com
*/
@@ -44,6 +44,8 @@ public class EntityManagerFactoryAccessor implements IEntityManagerFactoryAccess
@PersistenceContext(unitName = "apiman-manager-api-jpa")
private EntityManager pcEm;
+ private final ThreadLocal<EntityManager> threadLocal = new ThreadLocal<>();
+
/**
* Constructor.
*/
@@ -93,9 +95,6 @@ public class EntityManagerFactoryAccessor implements IEntityManagerFactoryAccess
throw t1;
}
}
-
- System.out.println("Hibernate properties init");
- //Persistence.getPersistenceUtil().
}
/**
@@ -107,20 +106,18 @@ public class EntityManagerFactoryAccessor implements IEntityManagerFactoryAccess
return emf;
}
- ThreadLocal<EntityManager> threadLocal = new ThreadLocal<>();
-
@Produces
public EntityManager getEntityManager() {
if (pcEm != null) {
- System.out.println("Using persistent context entity manager");
+ // System.out.println("Using persistent context entity manager");
return pcEm;
}
EntityManager threadLocalEm = threadLocal.get();
if (threadLocalEm != null && threadLocalEm.isOpen()) {
- System.out.println("Return thread local em");
+ // System.out.println("Return thread local em");
return threadLocalEm;
} else {
- System.out.println("Return new em");
+ // System.out.println("Return new em");
EntityManager newEm = emf.createEntityManager();
threadLocal.set(newEm);
return newEm;

MODIFY
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/JpaStorage.java
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/JpaStorage.java
@@ -102,6 +102,7 @@ import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;
import javax.transaction.Transactional;
+import javax.validation.constraints.NotNull;
import org.apache.commons.io.IOUtils;
@@ -1178,8 +1179,8 @@ public class JpaStorage extends AbstractJpaStorage implements IStorage, IStorage
}
@Override
- public List<ApiBean> getApisByTagName(String tagKey) {
- TypedQuery<ApiBean> query = getSession()
+ public List<ApiBean> getApisByTagName(@NotNull String tagKey) {
+ TypedQuery<ApiBean> query = getActiveEntityManager()
.createQuery(
"SELECT ApiBean FROM ApiBean avb "
+ "JOIN avb.tags tag "

fix(manager-api): featured APIs throwing an NPE when empty