0

在我使用 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:架构更新完成

这意味着它不能再读取表格了。这对你来说意味着什么吗?

4

2 回答 2

2

该错误是由 Hibernate 内部结构引起的,位于 org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73) 这可能意味着您的 JTA 配置或类有问题。由于您稍后尝试启动用户事务,因此您可能错误地将其标记为 JTA 而不是 resource_local。

这不是一个完整的答案,因为我不熟悉代码,但希望它有助于指出正确的方向。

于 2012-10-24T14:33:39.000 回答
0

根据NullPointerException你所说的和线,它被抛出:

EntityManager em = emf.createEntityManager();

我还假设它位于:

org.tutorial.LoginBean.authenticate(LoginBean.java:50).

emf的很可能是空的。根据您提供的代码,您每次需要 EntityManager 时都会创建 EntityManagerFactory。你不应该那样做。本质上这是一项昂贵的操作,您需要在应用程序启动期间执行一次,然后在整个应用程序中重复使用它。您可以查看获取 EntityManagerFactory 的最佳实践以了解如何解决该问题。


虽然我有你在这里,但我想就你提供的代码给你一些建议。它并没有真正做它应该做的一切。您至少应该有一个 try-finally 块来处理事务并在必要时回滚。以您的 create 方法为例:

public void create(Project project) {
        EntityManagerFactory emf =Persistence.createEntityManagerFactory("ResearchManagement.Data");

        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            //start the transaction
            tx.begin();
            em.persist(project);
            //commit it the transaction
            tx.commit();
        } finally {
            //if the transaction is still active then
            //than something bad happened, so we need to
            //roll it back
            if (tx.isActive()) {
               tx.rollback();
            }
            //close out the entity manager instance, we are
            //done with it here
            em.close();
        }
    }

最后一件事没有那么大,它更多的是关于格式化,老实说,它可能只是你复制粘贴到你的问题中的方式导致了它。但是,如果你的代码中确实有这么多的白人节奏,那会让事情变得更难掌握。使用空白从本质上使事情更容易阅读。太多了,滚动起来很痛苦,太少了,一切都被挤在一起,更难处理。


编辑:您评论说:

谢谢你的回答和关于我的代码的提示。但是,我调试了我的应用程序,发现 emf 不是 null 。我仍然在该行收到错误:EntityManager em = emf.createEntityManager();

所以我的猜测是 emf 返回一个 null entityManager。还是不明白为什么

如果emf.createEntityManager()只返回一个 null EntityManager,那么您将不会有指向该特定行的堆栈跟踪,而是指向在实例em本身上调用方法的行(例如 may em.getTransaction())。所以要么emf是空的,要么不应该为空的东西emf.createEntityManager()是空的。只有两个选择。

于 2012-10-23T20:45:44.460 回答