代码之家  ›  专栏  ›  技术社区  ›  Jako

JPA NamedQuery不在Glassfish4中(但在Glassfish3中可用)

  •  1
  • Jako  · 技术社区  · 6 年前

    我正在使用EJB和JPA(EclipseLink)将一个应用程序从Glassfish3迁移到Glassfish4。大多数正在新服务器上工作,但由于某种原因,此命名查询(其中对象在JPQL语句中实例化)不起作用:

    @NamedQuery(
        name = "countByUserType", 
        query = "SELECT new org.util.CountBean(ev.user.type, count(ev.user.id)) 
                 FROM Event ev WHERE ev.idTs = :idTs GROUP BY ev.user.type"
    )
    

    我的类CountBean是一个简单的POJO类

    public class CountBean{
        String label;
        Long count;
        public CountBean(String label, Long count) {
            this.label = label;
            this.count = count;
        }
    }
    

    我收到的错误消息如下:

     Caused by: Exception [EclipseLink-6168] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.QueryException
     Exception Description: Query failed to prepare, unexpected error occurred: [java.lang.NullPointerException].
     Internal Exception: java.lang.NullPointerException
     Query: ReportQuery(name="countByUserType" referenceClass=Event jpql="SELECT new org.util.CountBean(ev.user.type, count(ev.user.idU), 0l) FROM Event ev WHERE ev.idTs = :idTs GROUP BY ev.user.type")
        at org.eclipse.persistence.exceptions.QueryException.prepareFailed(QueryException.java:1584)
        at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:680)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:888)
        at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:613)
        at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:341)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1107)
        at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:522)
        at org.un.sers.repository.EventFacade.countRoomsBooked(EventFacade.java:37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        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:46)
        at sun.reflect.GeneratedMethodAccessor90.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        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.GeneratedMethodAccessor91.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        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)
        ... 35 more
     Caused by: java.lang.NullPointerException
        at org.eclipse.persistence.queries.ConstructorReportItem.initialize(ConstructorReportItem.java:167)
        at org.eclipse.persistence.queries.ReportQuery.prepare(ReportQuery.java:1058)
        at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:661)
        ... 69 more
    

    如果有人遇到过类似的问题,或者如果我发布的代码中有什么不对劲的地方,我会很感激。同样,当我将应用程序部署在Glassfish 3上时,这一点也可以实现,但在Glassfish 4上则不行,它所连接的数据库是Oracle 11g。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Youcef LAIDANI    6 年前

    这是bug 在旧版本的eclipselink中,检查以下内容:


    解决方案

    更好的解决方法是升级到最新版本2.7.1 Latest Release


    旁边我想用 Payara Server 而不是GlassFish 4服务器 ,修复了许多错误。我对这台服务器有很好的经验,它现在比GlassFish性能更好,速度更快。