本文介绍了createEntityManager在org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus中抛出java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我部署我的hibernate应用程序时,我收到了这个堆栈跟踪

  java.lang.NullPointerException 
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:72)[hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1]
at org.hibernate。 engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:114)[hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1]
at org.hibernate.engine.transaction。 internal.jta.CMTTransaction.join(CMTTransaction.java:149)[hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1]
at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl。 (hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1)
at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:170)[hibernate-entitymanager-4.0 .0.Beta1.jar:4.0.0.Beta1]
at org.hibernate.ejb.EntityManagerImpl。< init>(EntityManagerImpl.java:90)[hibernate-enti tymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
在org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:112)[hibernate-entitymanager-4.0.0.Beta1.jar: 4.0.0.Beta1]
at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:107)[hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
在com.mycompany.myapp.common.persistence.HibernateUtil。< clinit>(HibernateUtil.java:53)[classes:]
at com.mycompany.myapp.common.businessobjects.ServerSettings.GetServerSettings(ServerSettings.java :247)[classes:]
at com.mycompany.myapp.common.servlet.SecurityFilter.init(SecurityFilter.java:55)[classes:]
at org.apache.catalina.core.ApplicationFilterConfig .getFilter(ApplicationFilterConfig.java:447)[jbossweb-7.0.0.CR4.jar:7.0.0.Final]
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3245)[ jbossweb-7.0.0.CR4.jar:7.0.0.Final]
在org.apache .catalina.core.StandardContext.start(StandardContext.java:3836)[jbossweb-7.0.0.CR4.jar:7.0.0.Final]
at org.jboss.as.web.deployment.WebDeploymentService.start (WebDeploymentService.java:70)[jboss-as-web-7.0.0.Final.jar:7.0.0.Final]
at org.jboss.msc.service.ServiceControllerImpl $ StartTask.run(ServiceControllerImpl.java :1765)
at org.jboss.msc.service.ServiceControllerImpl $ ClearTCCLTask.run(ServiceControllerImpl.java:2291)
at java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:886 )[:1.6.0_26]
at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:908)[:1.6.0_26]
at java.lang.Thread.run(Thread .java:680)[:1.6.0_26]

在我的HibernateUtil类中打了一个NPE,第53行是创建EntityManager的行 - 它看起来像createEntityManager()返回null。代码看起来像

  private static EntityManagerFactory entityManagerFactory = null; 
private static EntityManager entityManager = null;

static {
try {

entityManagerFactory = Persistence.createEntityManagerFactory(primary);
entityManager = entityManagerFactory.createEntityManager();

我的persistence.xml看起来像

 <?xml version =1.0encoding =UTF-8?> 
< persistence version =2.0xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-实例xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\"> < persistence-unit name =primary> < JTA - 数据 - 源>的java:JBoss的/数据源/ MySqlDS< / JTA - 数据 - 源>

<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.ejb.interceptor.session_scopedvalue =com.mycompany.myapp.common.persistence.BusinessObjectInterceptor/>
< / properties>




为什么createEntityManager()给NPE?

解决方案

Stacktrace清楚地表明Hibernate无法加入JTA事务。此外,< jta-data-source> Persistence.createEntityManagerFactory()的联合看起来很奇怪 - 支持JTA EntityManager 通常由应用程序服务器创建并通过JNDI从它获得。



如果您真的想使用resource-本地事务而不是JTA,您需要声明<非-jta-data-source>

I'm getting this stack trace when I deploy my hibernate app

java.lang.NullPointerException
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:72) [hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:114) [hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) [hibernate-core-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1197) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:170) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:90) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:112) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:107) [hibernate-entitymanager-4.0.0.Beta1.jar:4.0.0.Beta1]
    at com.mycompany.myapp.common.persistence.HibernateUtil.<clinit>(HibernateUtil.java:53) [classes:]
    at com.mycompany.myapp.common.businessobjects.ServerSettings.GetServerSettings(ServerSettings.java:247) [classes:]
    at com.mycompany.myapp.common.servlet.SecurityFilter.init(SecurityFilter.java:55) [classes:]
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:447) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3245) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3836) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.0.0.Final.jar:7.0.0.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1765)
    at org.jboss.msc.service.ServiceControllerImpl$ClearTCCLTask.run(ServiceControllerImpl.java:2291)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_26]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_26]
    at java.lang.Thread.run(Thread.java:680) [:1.6.0_26]

It is hitting a NPE in my HibernateUtil class, line 53 is a line that creates an EntityManager - it looks like createEntityManager() is returning a null. The code looks like

private static EntityManagerFactory entityManagerFactory = null;
private static EntityManager entityManager = null;

static {
    try {

        entityManagerFactory = Persistence.createEntityManagerFactory("primary");
        entityManager = entityManagerFactory.createEntityManager();

My persistence.xml looks like

<?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="primary"> <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>

  <properties>
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
     <property name="hibernate.ejb.interceptor.session_scoped" value="com.mycompany.myapp.common.persistence.BusinessObjectInterceptor"/>
           </properties>

Any ideas why createEntityManager() giving the NPE?

解决方案

Stacktrace clearly indicates that Hibernate cannot join JTA transaction. Moreover, comination of <jta-data-source> and Persistence.createEntityManagerFactory() looks strange - JTA-capable EntityManager is usually created by application server and obtained from it via JNDI.

If you actually want to use resource-local transactions rather than JTA, you need to declare <non-jta-data-source> instead.

这篇关于createEntityManager在org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus中抛出java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 22:08