在JPA 2.0中自动注入实体管理器

时间:2016-07-21 15:58:57

标签: jpa

我正在处理一个JSF 2.1项目的一个非常奇怪的问题 我正在使用IBM Rational Application Developer for WebSphere Software 9.1和WebSphere Application Server V8.5.5 我已经使用JSF 2.1版创建了一个动态JSF Web项目,并使用EJB 3.1和JPA 2创建了包含我的实体和EJB的EJB项目,并且我已将CDI(内容和依赖注入)方面包含在JPA和JSF中项目

我正在处理的项目是一个简单的新闻模块,我应该在新闻项目和作者上执行添加,搜索,更新和删除操作。

当我手动设置实体管理器时,一切正常,但是当我不手动设置实体管理器并让它自动注入时,每当我尝试访问实体时,我都会得到NullPointerException管理器。

我的代码如下

我的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="newsProjectPersistence" transaction-type="JTA">
        <jta-data-source>jndi/NewsProj</jta-data-source>
        <class>model.User</class>
        <class>model.Address</class>
        <class>model.Author</class>
        <class>model.NewsItem</class>
    </persistence-unit>
</persistence>

我的Controller.java类(EJB)

我有两个JSP页面,Login.jsp和Index.jsp

我的Login.jsp

我的Login.java(网页代码/托管bean)

我的Index.jsp

我的Index.java(网页代码/托管bean)

例外是:

        [7/24/16 16:53:32:544 EET] 00000092 annotation    W com.ibm.ws.webcontainer.annotation.WASAnnotationHelper inject SRVE8042E: An internal error caused the reference context that enables injection to not be initialized properly.
[7/24/16 16:53:35:765 EET] 00000092 ErrorPageWrit E   An exception occurred
        javax.faces.el.EvaluationException: org.apache.jasper.el.JspELException: /Login.jsp(63,9) '#{pc_Login.doBtnLoginAction}' java.lang.NullPointerException
        at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:96)
        at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
        at javax.faces.component.UICommand.broadcast(UICommand.java:120)
        at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:973)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:275)
        at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1285)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:711)
        at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
        at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:172)
        at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:119)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1227)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:776)
        at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:458)
        at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
        at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1032)
        at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
        at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:909)
        at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
        at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:459)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:526)
        at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:312)
        at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
        at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
        at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
        at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
        at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
        at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
        at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
        at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
        at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1862)
    Caused by: org.apache.jasper.el.JspELException: /Login.jsp(63,9) '#{pc_Login.doBtnLoginAction}' java.lang.NullPointerException
        at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:79)
        at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
        ... 31 more
    Caused by: java.lang.NullPointerException
        at beans.Controller.checkAuthentication(Controller.java:157)
        at pagecode.Login.doBtnLoginAction(Login.java:72)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
        at java.lang.reflect.Method.invoke(Method.java:611)
        at org.apache.el.parser.AstValue.invoke(AstValue.java:268)
        at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
        at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:70)
        ... 32 more

我试图提供所有信息,所以我可以得到任何帮助,因为我已经尝试了我在网上找到的所有内容并且问了很多,而且一切都没有用。

我得到的异常主要是因为启用注入的引用上下文没有正确初始化,我不明白为什么。据我所知,我做的一切都是正确的,所以任何帮助都会受到赞赏

[编辑]:当我使用emf = Persistence.createEntityManagerFactory("newsProjectPersistence");em = emf.createEntityManager();时(当我手动设置实体管理器时),一切运行正常,但当我删除这些行时,如{{ 1}}每当我尝试访问实体管理器时,我都会得到Controller.java

另外,据我了解,我不需要调用NullPointerException来反映数据库的更改,并且应该自动刷新更改。但是,这不是这种情况。当我手动设置实体管理器并且所有数据库操作都正常时,我需要手动刷新以反映数据库的更改。我不知道这是否与未启用自动注射的事实有关。

0 个答案:

没有答案