gpt4 book ai didi

tomcat - 尝试使用 JPA 从数据库读取数据时出现 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-28 23:46:13 25 4
gpt4 key购买 nike

我是一名学生,我有一个嵌入了 jbpm 5 框架的 Web 应用程序(这个问题不是关于任何 jbpm 5 相关主题,而是关于 JPA 和 jbpm 类在 MySql 数据库上的持久性)。此 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”的表中读取数据,该表由数据库上的持久性工作生成,即使该表对应' s java 类未在 persistance.xml 上声明(这对我来说很奇怪,但表已创建并包含数据!!但是当我遇到异常时我自己做了,但这不是解决方案)。该表的 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。伙计们,有什么想法吗?干杯。

最佳答案

Caused by: java.lang.NullPointerException at entities.dao.OrganizationalEntityDao.readById(OrganizationalEntityDao.java:20) at entities.beans.ProcessBean.requestPage(ProcessBean.java:121)

您的代码已损坏。 OrganizationalEntityDao.java 的第 20 行是什么?我怀疑您会发现一个潜在的 NPE——您正在遇到的那个。

关于tomcat - 尝试使用 JPA 从数据库读取数据时出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14053567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com