EntityManager.createQuery() 抛出 NullPointerException

2024-04-03

每次都会遇到 NullPointerExceptionEntityManager.createQuery() or EntityManager.createNamedQuery()叫做。

我正在使用 Glassfish 4.0、Hibernate 4.3.5 和 EJB。

我有以下层次结构:

DaoTemplate,我有 PersistenceContext 和通用方法

@Singleton
public class DaoTemplate<T extends IdEntityInterface> {
    @PersistenceContext(unitName = "pu_name")
    protected EntityManager em;
    ....
}

具有构造函数的抽象类,因为 bean 不能扩展另一个 bean。

public abstract class AbstractDaoTemplate<T extends IdEntityInterface> extends    DaoTemplate<T> {

    public AbstractDaoTemplate(Class<T> entityClass) {
        super(entityClass);
    }

}

然后我就有了道。

@Singleton
public class SomeDao extends AbstractDaoTemplate<SomeEntity> {

    public SomeDao() {
        super(SomeEntity.class)
    }        

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public List<SomeEntity> someDaoMethod() {
        ...
        em.createQuery("SomeQuery");
        ...   
    }
}

当调用 someDaoMethod 时,会抛出 NullPointerExceptionem.createQuery("SomeQuery") but em不为空。

我也尝试过打电话em.createQuery("SomeQuery")在调试器窗口中,输出如下:

Exception occurred in target VM:  
java.lang.NullPointerException: 
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
    at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:338)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:197)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:455)
    at eu.comcom.kweportal.dao.ShipmentDao.getActiveOldShipments(ShipmentDao.java:196)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(Abstrac tEJBRequestScopeActivationInterceptor.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.GeneratedMethodAccessor77.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLoca    lObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy542.getActiveOldShipments(Unknown Source)
    at eu.comcom.kweportal.dao.__EJB31_Generated__ShipmentDao__Intf____Bean__.getActiveOldShipments(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jboss.weld.util.reflection.Reflections.invokeAndUnwrap(Reflections.java:396)
    at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:108)
    at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56)
    at org.jboss.weld.bean.proxy.InjectionPointPropagatingEnterpriseTargetBeanInstance.invoke(InjectionPointPropagatingEnterpriseTargetBeanInstance.java:63)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:101)
    at eu.comcom.kweportal.dao.ShipmentDao$Proxy$_$$_Weld$EnterpriseProxy$.getActiveOldShipments(Unknown Source)
    at eu.comcom.kweportal.schedule.ActualTimeScheduler.updatePastMilestonesTimes(ActualTimeScheduler.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke (AbstractEJBRequestScopeActivationInterceptor.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundTimeout(SystemInterceptorProxy.java:145)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
    at com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:3993)
    at com.sun.ejb.containers.EJBTimerService.deliverTimeout(EJBTimerService.java:1199)
    at com.sun.ejb.containers.EJBTimerService.access$000(EJBTimerService.java:89)
    at com.sun.ejb.containers.EJBTimerService$TaskExpiredWork.run(EJBTimerService.java:1919)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

和 persistence.xml:

<?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="pu_name" transaction-type="JTA">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <jta-data-source>jdbc/kweportal</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
          <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>
      </properties>
  </persistence-unit>
</persistence>

有什么想法可能是错的吗?


我自己已经弄清楚了。

persistence.xml 必须如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/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_1.xsd">
  <persistence-unit name="pu_name" transaction-type="JTA">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <jta-data-source>jdbc/kweportal</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
          <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>
          <property name="hibernate.transaction.jta.platform"       value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
      </properties>
</persistence-unit>
</persistence>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EntityManager.createQuery() 抛出 NullPointerException 的相关文章

随机推荐

  • 如何在Python OpenCV中删除轮廓内部的轮廓?

    Python中的OpenCV提供了以下代码 regions hierarchy cv2 findContours binary image cv2 RETR LIST cv2 CHAIN APPROX SIMPLE for region i
  • Android:无法找到或加载主类org.gradle.wrapper.GradleWrapperMain

    我正在尝试在 GitLab CI 上构建我的项目 但不幸的是 我在运行器中不断收到此错误 Error Could not find or load main class org gradle wrapper GradleWrapperMai
  • 如何删除/卸载嵌套的反应组件

    我想卸载单个反应组件 该组件属于总共包含三个组件的父组件 父组件有这个渲染函数 render function return div div
  • 使用 scrapy 抓取多个页面

    我正在尝试使用 scrapy 抓取多个网页 页面的链接如下 http www example com id some number 在下一页中 末尾的数字减少了1 所以我正在尝试构建一个蜘蛛 它可以导航到其他页面并抓取它们 我的代码如下 i
  • Rails 字体 CORS 政策

    我无法为 CORS 策略加载此字体 Folder app assets fonts Inter UI var woff2 Error 访问字体位于 http localhost 3000 assets Inter UI var e2e323
  • android中处理html webview的onclick功能

    我正在尝试处理 android referred 中 html 的 onclick在此输入链接描述 https stackoverflow com questions 4065312 detect click on html button
  • iOS 中如何将代码块分派到同一个线程?

    问题的主要方面 这是关于 iOS 的 我能否以某种方式分派代码块 使它们全部 a 在后台运行并且 b 在同一个线程上运行 我想在后台运行一些耗时的操作 但这些操作必须在同一线程上运行 因为它们涉及资源 而不能在线程之间共享 如果需要 更多技
  • 在 ASP.NET Core 中,如何在Configure() 中使用在ConfigureServices() 中设置的服务?

    我编写了一项有助于配置的服务 该服务在 Startup 类的 ConfigureServices 方法中设置为 public void ConfigureServices IServiceCollection services servic
  • Java JComboBox 自动完成[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • JSF 服务层

    我不确定我在 JSF 中使用 MVC 环境的方法是否是最好的方法 因为我试图充分利用 JSF 所以我想知道我的服务层 或模型 用 MVC 术语来说 应该如何 设计 我知道视图与控制器的比例应该是 1 比 1 排除例外 现在我应该以什么方式设
  • 从文件中 read() - 阻塞行为与非阻塞行为

    假设我们使用以下命令打开了一个文件fopen 并从收到的文件指针中 使用以下命令获取文件描述符fileno 然后我们做很多 gt 10 8 随机read 该文件中相对较小的块 大小在 4 字节到 10 KB 之间 这是预期的行为吗read
  • 测试有效输入 Python

    因此 我有一个完成的程序 它接受带有银行帐户信息的输入文件并对其进行解析并允许使用一些不同的实用程序 其中一个实用程序是将事务添加到 数据库 只是一个日志文件 程序提示用户输入 w 或 d 然后输入一个金额 浮点数 这代表存款或取款 X 金
  • Vstest.console.exe 找不到 nunit 测试

    我有 VS 2017 和 net 4 6 尝试使用 vstest console 通过 cmd 提示符运行时 vstest console exe bin Release Tests dll UseVsixExtensions true 我
  • 如何用css制作透明的羽毛首选项

    我想为一些图像添加羽毛 这是我想要最终得到的结果 我意识到这个问题之前已经发布过 并且我已经使用例如box shadow attr 但我对这个方法有一个问题 我要把这张照片放在webm 所以背景并不总是保持不变 这就是为什么我必须使羽毛透明
  • 如何将列表拆分为大小相等的块?

    如何将任意长度的列表拆分为大小相等的块 See also How to iterate over a list in chunks https stackoverflow com q 434287 To chunk strings see
  • 带 json 参数的 Curl GET 请求

    我正在尝试通过 cURL 从命令提示符向远程 REST API 发送 GET 请求 如下所示 curl X GET H Content type application json H Accept application json http
  • Flutter 如何将数据列表保存到本地存储

    我正在 flutter 上开发电影发现应用程序 我需要将正在播放的电影列表保存在本地存储上以供离线使用 那么我该怎么做 Future gt getNowPlayingMovies 异步 最终字符串现在正在播放 https api themo
  • 如何动态判断Objective-C属性类型?

    我正在尝试动态确定 Objective C 中属性的类型 根据我在本网站和其他地方读到的内容 我相信我正在做正确的事情 但是 我的代码不起作用 下面的代码片段演示了这个问题 尝试获取 backgroundColor 和 frame 的属性信
  • 使用 API 发布不包含图片、标题或元描述的链接 PHP

    使用 API 发帖时 我收到以下消息 https www facebook com weather warnings posts 329128793830700 https www facebook com weather warnings
  • EntityManager.createQuery() 抛出 NullPointerException

    每次都会遇到 NullPointerExceptionEntityManager createQuery or EntityManager createNamedQuery 叫做 我正在使用 Glassfish 4 0 Hibernate