JPA NamedQuery 不在 Glassfish4 中(但在 Glassfish3 中有效)
JPA NamedQuery not in Glassfish4 (but works in Glassfish3)
我正在将使用 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"
)
我的 class CountBean 是一个简单的 POJO class
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 时,这仍然有效。
这是旧版本 eclipselink 中的错误,请检查:
- https://bugs.eclipse.org/bugs/show_bug.cgi?id=446861
- https://bugs.eclipse.org/bugs/show_bug.cgi?id=444561
解决方案
最好的解决方法是升级到最新版本2.7.1 Latest Release
此外,我想使用 Payara Server 而不是 GlassFish 4 Server,修复了很多错误我对这个服务器有很好的体验,它更现在性能比 GlassFish 更快。
我正在将使用 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"
)
我的 class CountBean 是一个简单的 POJO class
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 时,这仍然有效。
这是旧版本 eclipselink 中的错误,请检查:
- https://bugs.eclipse.org/bugs/show_bug.cgi?id=446861
- https://bugs.eclipse.org/bugs/show_bug.cgi?id=444561
解决方案
最好的解决方法是升级到最新版本2.7.1 Latest Release
此外,我想使用 Payara Server 而不是 GlassFish 4 Server,修复了很多错误我对这个服务器有很好的体验,它更现在性能比 GlassFish 更快。