在我使用 JPA + hibernate 的应用程序中。但是,当我尝试访问数据库时,出现以下错误:
java.lang.NullPointerException: javax.faces.FacesException:
#{loginBean.authenticate}: java.lang.NullPointerException
当我执行该行时发生异常:EntityManager em = emf.createEntityManager();
下面你会看到完整的服务器日志。
18:21:48,174 INFO [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (http--0.0.0.0-8080-1) HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
18:21:48,177 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (http--0.0.0.0-8080-1) HHH000397: Using ASTQueryTranslatorFactory
18:21:48,189 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000228: Running hbm2ddl schema update
18:21:48,191 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000102: Fetching database metadata
18:21:48,193 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000396: Updating schema
18:21:48,195 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000232: Schema update complete
18:22:08,153 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http--0.0.0.0-8080-1) #{loginBean.authenticate}: java.lang.NullPointerException: javax.faces.FacesException: #{loginBean.authenticate}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_32]
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]
... 21 more
Caused by: java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:176) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:125) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:120) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.Handlers.UserHandler.getAllUsers(UserHandler.java:32) [classes:]
at org.tutorial.LoginBean.authenticate(LoginBean.java:50) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_32]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_32]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.6.0_32]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.6.0_32]
at org.apache.el.parser.AstValue.invoke(AstValue.java:262) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.0.13.Final.jar:]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
... 22 more
我的猜测是,出于某种原因,它与实体管理器有关。我对使用 hibernate 和 JPA 真的很陌生。我还附上了一个课程,向您展示我如何访问数据库。有人能弄清楚我做错了什么吗?
public class ProjectHandler {
public void create(Project project) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(project);
em.getTransaction().commit();
em.close();
}
public List<Project> getAllProjects() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("SELECT p FROM Project p");
List<Project> projects = query.getResultList();
em.getTransaction().commit();
em.close();
return projects;
}
public int deleteByProjectName(String projectName) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ResearchManagement.Data");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query q = em.createQuery("DELETE FROM Project p WHERE p.projectName = :u");
q.setParameter("u", projectName );
int deleted = q.executeUpdate();
em.getTransaction().commit();
em.close();
return deleted;
}
}
谢谢你的回答和关于我的代码的提示。但是,我调试了我的应用程序,发现 emf 不是 null 。我仍然在该行收到错误:EntityManager em = emf.createEntityManager();
所以我的猜测是 emf 返回一个 null entityManager。还是不明白为什么
当我运行应用程序时,我注意到并且看起来很奇怪的服务器日志。它读取 pesistence 单元并连接到读取 corerclty 所有表的数据库:
12:02:06,094 INFO [org.hibernate.validator.util.Version](MSC 服务线程 1-3)Hibernate Validator 4.2.0.Final
12:02:06,328 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate](MSC 服务线程 1-3)HHH000228:运行 hbm2ddl 模式更新
12:02:06,328 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate](MSC 服务线程 1-3)HHH000102:获取数据库元数据
12:02:06,328 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate](MSC 服务线程 1-3)HHH000396:更新模式
12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.departments
12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[电话号码、部门 ID、姓名、城市]
12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[]
12:02:06,343 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[主要]
12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.employees
12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[电话号码、部门 ID、雇员 ID、姓氏、名字]
12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[fk9d39ef85d77ba982]
12:02:06,359 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[fk9d39ef85d77ba982,主要]
12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.partners
12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[partnerid、name、contactmail、city]
12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[]
12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[主要]
12:02:06,374 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.projects
12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[部门 ID、持续时间、描述、项目名称、项目 ID]
12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[fkc479187ad77ba982]
12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[primary,fkc479187ad77ba982]
12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.projects_has_employees
12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[projects_projectid, employees_employeeid]
12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[fk294f111bcd3ec025,fk294f111b92772ef4]
12:02:06,390 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[fk294f111bcd3ec025,primary,fk294f111b92772ef4]
12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.projects_has_partners
12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[partners_partnerid,projects_projectid]
12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[fk8d741055cd3ec025,fk8d7410555290eb4]
12:02:06,406 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[fk8d741055cd3ec025,fk8d7410555290eb4,主要]
12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.users
12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[用户名、用户 ID、密码]
12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[]
12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[主要]
12:02:06,421 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000261:找到的表:researchprojects.hibernate_sequence
12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000037:列:[next_val]
12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000108:外键:[]
12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.TableMetadata](MSC 服务线程 1-3)HHH000126:索引:[]
12:02:06,437 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate](MSC 服务线程 1-3)HHH000232:架构更新完成
但是,当我尝试读取用户表 (getallUsers() ) 时,它会创建 emf 并在日志中看到:
12:03:53,346 信息 [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (http--0.0.0.0-8080-1) HHH000397:使用 ASTQueryTranslatorFactory
12:03:53,347 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000228:运行 hbm2ddl 架构更新
12:03:53,347 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000102:获取数据库元数据
12:03:53,347 信息 [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000396:更新架构
12:03:53,347 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--0.0.0.0-8080-1) HHH000232:架构更新完成
这意味着它不能再读取表格了。这对你来说意味着什么吗?