尝试使用JPA从数据库读取数据时的java.lang.NullPointerException

时间:2012-12-27 10:59:48

标签: tomcat jpa persistence

我是学生,我有一个嵌入了jbpm 5框架的Web应用程序(这个问题不是关于任何jbpm 5相关主题,而是关于JPA和MySql数据库上jbpm类的持久性)。 此Web应用程序应在使用Bitronix事务管理器配置的tomcat 7上运行。 JPA用于使用2个持久性单元来持久化jbpm相关类,这里是persistance.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" xmlns:orm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/persistence">
     <persistence-unit name="org.jbpm.persistence.jpa" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:comp/env/jdbc/testDS1</jta-data-source>
        <class>org.drools.persistence.info.SessionInfo</class>
        <class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
        <class>org.jbpm.persistence.processinstance.ProcessInstanceEventInfo</class>
        <class>org.drools.persistence.info.WorkItemInfo</class>
        <class>org.jbpm.process.audit.ProcessInstanceLog</class>
        <class>org.jbpm.process.audit.NodeInstanceLog</class>
        <class>org.jbpm.process.audit.VariableInstanceLog</class>       


        <class>org.jbpm.task.Task</class>
        <class>org.jbpm.task.Comment</class>
        <class>org.jbpm.task.Attachment</class>
        <class>org.jbpm.task.I18NText</class>
        <class>org.jbpm.task.SubTasksStrategy</class>
        <class>org.jbpm.task.Deadline</class>
        <class>org.jbpm.task.Escalation</class>
        <class>org.jbpm.task.Reassignment</class>
        <class>org.jbpm.task.Notification</class>
        <class>org.jbpm.task.BooleanExpression</class>
        <class>org.jbpm.task.User</class>
        <class>org.jbpm.task.PeopleAssignments</class>


        <properties>

             <!-- H2 dialect  -->

            <!--<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>-->

             <!-- Oracle dialect   
               <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> 
              -->
             <!-- mysql dialect --> 

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

            <property name="hibernate.connection.autocommit" value="false"/>
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup"/>
        </properties>


    </persistence-unit>
    <persistence-unit name="org.jbpm.task">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>org.jbpm.task.Attachment</class>
        <class>org.jbpm.task.Content</class>
        <class>org.jbpm.task.BooleanExpression</class>
        <class>org.jbpm.task.Comment</class>
        <class>org.jbpm.task.Deadline</class>
        <class>org.jbpm.task.Comment</class>
        <class>org.jbpm.task.Deadline</class>
        <class>org.jbpm.task.Delegation</class>
        <class>org.jbpm.task.Escalation</class>
        <class>org.jbpm.task.Group</class>
        <class>org.jbpm.task.I18NText</class>
        <class>org.jbpm.task.Notification</class>
        <class>org.jbpm.task.EmailNotification</class>
        <class>org.jbpm.task.EmailNotificationHeader</class>
        <class>org.jbpm.task.PeopleAssignments</class>
        <class>org.jbpm.task.Reassignment</class>
        <class>org.jbpm.task.Status</class>
        <class>org.jbpm.task.Task</class>
        <class>org.jbpm.task.TaskData</class>
        <class>org.jbpm.task.SubTasksStrategy</class>
        <class>org.jbpm.task.OnParentAbortAllSubTasksEndStrategy</class>
        <class>org.jbpm.task.OnAllSubTasksEndParentEndStrategy</class>

        <class>org.jbpm.task.User</class>



        <class>org.drools.persistence.info.SessionInfo</class>
        <class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
        <class>org.jbpm.persistence.processinstance.ProcessInstanceEventInfo</class>
        <class>org.drools.persistence.info.WorkItemInfo</class>


        <properties>
             <!-- sample H2 configuration -->
             <!--
            <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:mem:droolsflow" />
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="sdfsdf"/> -->


            <!-- Sample Oracle Configuration  

             <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@oraclehost:1521:jbpm5" />
            <property name="hibernate.connection.username" value="user"/>
            <property name="hibernate.connection.password" value="password"/>
           --> 

             <!-- Sample mysql Configuration --> 


             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jbpm5webexpl" />
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="sdfsdf"/>


            <property name="hibernate.connection.autocommit" value="false" />
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

持久性工作得很好,我得到了数据库填充数据。现在我想从那个数据中读取特别是来自名为“organizationalentity”的表,这是由数据库上的持久性工作生成的,即使该表对应'没有在persistance.xml上声明s java类(这对我来说很奇怪,但是表创建并包含数据!!但是当我得到异常时我自己做了但不是解决方案)。该表的java类实际上存在于maven引用的jar上而不是我自己编写的包上(这就是我不能在其上添加一些NamedQueries的原因,因为这个类是.class而不是的.java ..)

所以我在我的类路径中添加了这个新的OrganizationalEntityDao.java类:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jbpm.task.OrganizationalEntity;

public class OrganizationalEntityDao {


    @PersistenceContext(unitName = "org.jbpm.task")
    static EntityManager em;

        public static OrganizationalEntity readById(int id) {
        return em.find(OrganizationalEntity.class,id); 
    }
}

当我运行我的应用程序时,我收到了这个错误:

javax.el.ELException: /index.xhtml @142,119 listener="#{processBean.requestPage}": java.lang.NullPointerException
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
    at com.sun.faces.facelets.tag.jsf.core.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxHandler.java:450)
    at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113)
    at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:760)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1770)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at entities.dao.OrganizationalEntityDao.readById(OrganizationalEntityDao.java:20)
    at entities.beans.ProcessBean.requestPage(ProcessBean.java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    ... 27 more

我应该说我想知道在tomcat上使用JPA来持久化实体(作为EJB工作)。我没有使用JDBC来完成这项工作,因为JPA已被使用。 好人,任何想法?欢呼声。

1 个答案:

答案 0 :(得分:0)

  

引起:java.lang.NullPointerException       在entities.dao.OrganizationalEntityDao.readById(OrganizationalEntityDao.java:20)       在entities.beans.ProcessBean.requestPage(ProcessBean.java:121)

您的代码已损坏。 OrganizationalEntityDao.java第20行的内容是什么?我怀疑你会找到一个潜在的NPE - 你遇到的那个。