EntityManager抛出NullPointerException

时间:2018-04-03 13:17:36

标签: java java-ee wildfly weld

我正在运行keycloak Photoz example。当我部署到Wildfly时,EntityManager没有被注入,我得到一个NullPointerException(下面)。我尝试了一些不同的策略 - 调整persistence.xml(删除LOCAL_RESOURCE)并切换到@PersistenceContext属性而没有运气。由于这些例子是罐装的,人们会认为我离得太远了?我不太了解Java以便调试它。这似乎与我的Wildfly安装有关?但该示例旨在在wildfly(mvn clean package wildfly:deploy)中运行。有什么建议吗?

注意:我可以手动创建EntityManager,如下所示。还要注意,我必须调用init(),所以在这个应用程序中DI通常出现问题...

Resources r = new Resources();
r.init();
EntityManager entityManager = r.createEntityManager();

堆栈跟踪:

Caused by: java.lang.NullPointerException
keycloak_1    |     at org.keycloak.example.photoz.album.AlbumService.findAll(AlbumService.java:86)
keycloak_1    |     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
keycloak_1    |     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
keycloak_1    |     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
keycloak_1    |     at java.lang.reflect.Method.invoke(Method.java:498)
keycloak_1    |     at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
keycloak_1    |     at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
keycloak_1    |     at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
keycloak_1    |     at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
keycloak_1    |     at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:395)

服务

...
@Path("/album")
@Transaction
public class AlbumService {

    public static final String SCOPE_ALBUM_VIEW = "urn:photoz.com:scopes:album:view";
    public static final String SCOPE_ALBUM_DELETE = "urn:photoz.com:scopes:album:delete";

    @Inject
    private EntityManager entityManager;
...

    @GET
    @Produces("application/json")
    public Response findAll() {
        return Response.ok(this.entityManager.createQuery("from Album where userId = '" + request.getUserPrincipal().getName() + "'").getResultList()).build();
    }

...

... / SRC /主/资源/ META-INF / beans.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
    <interceptors>
        <class>org.keycloak.example.photoz.util.TransactionInterceptor</class>
    </interceptors>
</beans>

... / SRC /主/资源/ META-INF / persistence.xml中

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>org.keycloak.example.photoz.entity.Album</class>
        <class>org.keycloak.example.photoz.entity.Photo</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:~/keycloak-photoz-example" />
            <property name="hibernate.connection.user" value="sa" />
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

... / SRC /主/ JAVA /组织/ keycloak /示例/一些新的照片/ util的/ TransactionInterceptor.java

@Interceptor
@Transaction
public class TransactionInterceptor {

    @Inject
    private Instance<EntityManager> entityManager;

    @AroundInvoke
    public Object aroundInvoke(InvocationContext context) {
        EntityManager entityManager = this.entityManager.get();
        EntityTransaction transaction = entityManager.getTransaction();

        try {
            transaction.begin();
            Object proceed = context.proceed();
            transaction.commit();
            return proceed;
        } catch (Exception cause) {
            if (transaction != null && transaction.isActive()) {
                transaction.rollback();
            }
            throw new RuntimeException(cause);
        } finally {
            entityManager.close();
        }
    }
}

... / SRC /主/ JAVA /组织/ keycloak /示例/一些新的照片/ util的/ Resources.java

@ApplicationScoped
public class Resources {

    private EntityManagerFactory entityManagerFactory;

    @PostConstruct
    public void init() {
        entityManagerFactory = Persistence.createEntityManagerFactory("primary");
    }

    @PreDestroy
    public void dispose() {
        entityManagerFactory.close();
    }

    @RequestScoped
    @Produces
    public EntityManager createEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
}

0 个答案:

没有答案